diff options
author | Andy Gibbs <andyg1001@hotmail.co.uk> | 2012-11-17 19:15:38 +0000 |
---|---|---|
committer | Andy Gibbs <andyg1001@hotmail.co.uk> | 2012-11-17 19:15:38 +0000 |
commit | 02a176871d91bba3004e4f94b2d4d588ae4b2122 (patch) | |
tree | 526b40ca59194f0bdb548e3ac2ae64983861c4c2 /lib/Lex/Pragma.cpp | |
parent | b9971bada4eeae74883b61ba96fc6d983b6b0e7f (diff) |
Refactored duplicate string literal lexing code within Preprocessor, into a
common LexStringLiteral function. In doing so, some consistency problems have
been ironed out (e.g. where the first token in the string literal was lexed
with macro expansion, but subsequent ones were not) and also an erroneous
diagnostic has been corrected.
LexStringLiteral is complemented by a FinishLexStringLiteral function which
can be used in the situation where the first token of the string literal has
already been lexed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168266 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Pragma.cpp')
-rw-r--r-- | lib/Lex/Pragma.cpp | 99 |
1 files changed, 11 insertions, 88 deletions
diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index e7e6c37053..0c1c9dbee5 100644 --- a/lib/Lex/Pragma.cpp +++ b/lib/Lex/Pragma.cpp @@ -502,38 +502,9 @@ void Preprocessor::HandlePragmaComment(Token &Tok) { // Read the optional string if present. Lex(Tok); std::string ArgumentString; - if (Tok.is(tok::comma)) { - Lex(Tok); // eat the comma. - - // We need at least one string. - if (Tok.isNot(tok::string_literal)) { - Diag(Tok.getLocation(), diag::err_pragma_comment_malformed); - return; - } - - // String concatenation allows multiple strings, which can even come from - // macro expansion. - // "foo " "bar" "Baz" - SmallVector<Token, 4> StrToks; - while (Tok.is(tok::string_literal)) { - if (Tok.hasUDSuffix()) - Diag(Tok, diag::err_invalid_string_udl); - StrToks.push_back(Tok); - Lex(Tok); - } - - // Concatenate and parse the strings. - StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); - assert(Literal.isAscii() && "Didn't allow wide strings in"); - if (Literal.hadError) - return; - if (Literal.Pascal) { - Diag(StrToks[0].getLocation(), diag::err_pragma_comment_malformed); - return; - } - - ArgumentString = Literal.GetString(); - } + if (Tok.is(tok::comma) && !LexStringLiteral(Tok, ArgumentString, + /*MacroExpansion=*/true)) + return; // FIXME: If the kind is "compiler" warn if the string is present (it is // ignored). @@ -587,34 +558,9 @@ void Preprocessor::HandlePragmaMessage(Token &Tok) { return; } - // We need at least one string. - if (Tok.isNot(tok::string_literal)) { - Diag(Tok.getLocation(), diag::err_pragma_message_malformed); - return; - } - - // String concatenation allows multiple strings, which can even come from - // macro expansion. - // "foo " "bar" "Baz" - SmallVector<Token, 4> StrToks; - while (Tok.is(tok::string_literal)) { - if (Tok.hasUDSuffix()) - Diag(Tok, diag::err_invalid_string_udl); - StrToks.push_back(Tok); - Lex(Tok); - } - - // Concatenate and parse the strings. - StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); - assert(Literal.isAscii() && "Didn't allow wide strings in"); - if (Literal.hadError) - return; - if (Literal.Pascal) { - Diag(StrToks[0].getLocation(), diag::err_pragma_message_malformed); + std::string MessageString; + if (!FinishLexStringLiteral(Tok, MessageString, /*MacroExpansion=*/true)) return; - } - - StringRef MessageString(Literal.GetString()); if (ExpectClosingParen) { if (Tok.isNot(tok::r_paren)) { @@ -1090,50 +1036,27 @@ public: } PP.LexUnexpandedToken(Tok); + SourceLocation StringLoc = Tok.getLocation(); - // We need at least one string. - if (Tok.isNot(tok::string_literal)) { - PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); + std::string WarningName; + if (!PP.FinishLexStringLiteral(Tok, WarningName, /*MacroExpansion=*/false)) return; - } - - // String concatenation allows multiple strings, which can even come from - // macro expansion. - // "foo " "bar" "Baz" - SmallVector<Token, 4> StrToks; - while (Tok.is(tok::string_literal)) { - StrToks.push_back(Tok); - PP.LexUnexpandedToken(Tok); - } if (Tok.isNot(tok::eod)) { PP.Diag(Tok.getLocation(), diag::warn_pragma_diagnostic_invalid_token); return; } - // Concatenate and parse the strings. - StringLiteralParser Literal(&StrToks[0], StrToks.size(), PP); - assert(Literal.isAscii() && "Didn't allow wide strings in"); - if (Literal.hadError) - return; - if (Literal.Pascal) { - PP.Diag(Tok, diag::warn_pragma_diagnostic_invalid); - return; - } - - StringRef WarningName(Literal.GetString()); - if (WarningName.size() < 3 || WarningName[0] != '-' || WarningName[1] != 'W') { - PP.Diag(StrToks[0].getLocation(), - diag::warn_pragma_diagnostic_invalid_option); + PP.Diag(StringLoc, diag::warn_pragma_diagnostic_invalid_option); return; } if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2), Map, DiagLoc)) - PP.Diag(StrToks[0].getLocation(), - diag::warn_pragma_diagnostic_unknown_warning) << WarningName; + PP.Diag(StringLoc, diag::warn_pragma_diagnostic_unknown_warning) + << WarningName; else if (Callbacks) Callbacks->PragmaDiagnostic(DiagLoc, Namespace, Map, WarningName); } |