Merge pull request #53 from google/get-string-name-or-num

Add getStringNameOrNum() in TSourceLoc.
This commit is contained in:
John Kessenich 2015-08-06 22:06:34 -06:00
Родитель 306695360d bb63bd5e37
Коммит 3473a4b98f
3 изменённых файлов: 12 добавлений и 19 удалений

Просмотреть файл

@ -190,6 +190,13 @@ inline const TString String(const int i, const int base = 10)
struct TSourceLoc { struct TSourceLoc {
void init() { name = nullptr; string = 0; line = 0; column = 0; } void init() { name = nullptr; string = 0; line = 0; column = 0; }
// Returns the name if it exists. Otherwise, returns the string number.
std::string getStringNameOrNum(bool quoteStringName = true) const
{
if (name != nullptr)
return quoteStringName ? ("\"" + std::string(name) + "\"") : name;
return std::to_string(string);
}
const char* name; // descriptive name for this string const char* name; // descriptive name for this string
int string; int string;
int line; int line;

Просмотреть файл

@ -98,12 +98,8 @@ public:
void location(const TSourceLoc& loc) { void location(const TSourceLoc& loc) {
const int maxSize = 24; const int maxSize = 24;
char locText[maxSize]; char locText[maxSize];
if (loc.name != nullptr) { snprintf(locText, maxSize, ":%d", loc.line);
append(loc.name); append(loc.getStringNameOrNum(false).c_str());
snprintf(locText, maxSize, ":%d", loc.line);
} else {
snprintf(locText, maxSize, "%d:%d", loc.string, loc.line);
}
append(locText); append(locText);
append(": "); append(": ");
} }

Просмотреть файл

@ -620,13 +620,7 @@ int TPpContext::CPPinclude(TPpToken* ppToken)
std::ostringstream content; std::ostringstream content;
content << "#line " << forNextLine << " " << "\"" << sourceName << "\"\n"; content << "#line " << forNextLine << " " << "\"" << sourceName << "\"\n";
content << replacement << (replacement.back() == '\n' ? "" : "\n"); content << replacement << (replacement.back() == '\n' ? "" : "\n");
content << "#line " << directiveLoc.line + forNextLine << " "; content << "#line " << directiveLoc.line + forNextLine << " " << directiveLoc.getStringNameOrNum() << "\n";
if (directiveLoc.name != nullptr) {
content << "\"" << directiveLoc.name << "\"";
} else {
content << directiveLoc.string;
}
content << "\n";
pushInput(new TokenizableString(directiveLoc, content.str(), this)); pushInput(new TokenizableString(directiveLoc, content.str(), this));
} }
// At EOF, there's no "current" location anymore. // At EOF, there's no "current" location anymore.
@ -1015,13 +1009,9 @@ int TPpContext::MacroExpand(int atom, TPpToken* ppToken, bool expandUndef, bool
return 1; return 1;
case PpAtomFileMacro: { case PpAtomFileMacro: {
if (const char* current_file = parseContext.getCurrentLoc().name) { if (parseContext.getCurrentLoc().name)
parseContext.ppRequireExtensions(ppToken->loc, 1, &E_GL_GOOGLE_cpp_style_line_directive, "filename-based __FILE__"); parseContext.ppRequireExtensions(ppToken->loc, 1, &E_GL_GOOGLE_cpp_style_line_directive, "filename-based __FILE__");
sprintf(ppToken->name, "\"%s\"", current_file); sprintf(ppToken->name, "%s", ppToken->loc.getStringNameOrNum().c_str());
} else {
ppToken->ival = parseContext.getCurrentLoc().string;
sprintf(ppToken->name, "%d", ppToken->ival);
}
UngetToken(PpAtomConstInt, ppToken); UngetToken(PpAtomConstInt, ppToken);
return 1; return 1;
} }