]> granicus.if.org Git - clang/commitdiff
Change the Lexer::Diag method to not magically silence warnings,
authorChris Lattner <sabre@nondot.org>
Sat, 22 Nov 2008 02:02:22 +0000 (02:02 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 22 Nov 2008 02:02:22 +0000 (02:02 +0000)
force the caller to check instead.  This eliminates the need (and the
risk!) of weird null DiagnosticBuilder's floating around.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@59856 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/Diagnostic.h
include/clang/Lex/PreprocessorLexer.h
lib/Lex/Lexer.cpp
lib/Lex/PreprocessorLexer.cpp

index 00df94d1b18e4030740d856ef284d837138591fb..f782328ffd098cf7560bae06c18b9d55299cd06d 100644 (file)
@@ -261,7 +261,6 @@ class DiagnosticBuilder {
   explicit DiagnosticBuilder(Diagnostic *diagObj)
     : DiagObj(diagObj), NumArgs(0), NumRanges(0) {}
 public:
-  DiagnosticBuilder() : DiagObj(0) {}
   
   /// Copy constructor.  When copied, this "takes" the diagnostic info from the
   /// input and neuters it.
index 5d976ff08d54e14cf1b9993fbbd347e7a7c7c1c4..6275e01328e2f638d63f2997314667e9a0912f2f 100644 (file)
@@ -131,6 +131,13 @@ protected:
   void LexIncludeFilename(Token &Result);
   
 public:
+  
+  /// isLexingRawMode - Return true if this lexer is in raw mode or not.
+  bool isLexingRawMode() const { return LexingRawMode; }
+
+  /// getPP - Return the preprocessor object for this lexer.
+  Preprocessor *getPP() const { return PP; }
+  
   unsigned getFileID() const { 
     assert(PP &&
       "PreprocessorLexer::getFileID() should only be used with a Preprocessor");
index 4c25287b07c2b03c3a66fc2702295fb9d876859c..30875695899f05ab59fd48f1d7fbc38ae2da0d1e 100644 (file)
@@ -310,8 +310,6 @@ SourceLocation Lexer::getSourceLocation(const char *Loc) const {
 /// Diag - Forwarding function for diagnostics.  This translate a source
 /// position in the current buffer into a SourceLocation object for rendering.
 DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
-  if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
-    return DiagnosticBuilder();
   return PP->Diag(getSourceLocation(Loc), DiagID);
 }
 
@@ -345,11 +343,13 @@ static char DecodeTrigraphChar(const char *CP, Lexer *L) {
   if (!Res || !L) return Res;
   
   if (!L->getFeatures().Trigraphs) {
-    L->Diag(CP-2, diag::trigraph_ignored);
+    if (!L->isLexingRawMode())
+      L->Diag(CP-2, diag::trigraph_ignored);
     return 0;
   }
     
-  L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
+  if (!L->isLexingRawMode())
+    L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
   return Res;
 }
 
@@ -390,7 +390,7 @@ Slash:
           if (Tok) Tok->setFlag(Token::NeedsCleaning);
 
           // Warn if there was whitespace between the backslash and newline.
-          if (SizeTmp != 1 && Tok)
+          if (SizeTmp != 1 && Tok && !isLexingRawMode())
             Diag(Ptr, diag::backslash_newline_space);
           
           // If this is a \r\n or \n\r, skip the newlines.
@@ -534,7 +534,8 @@ FinishIdentifier:
       if (!Features.DollarIdents) goto FinishIdentifier;
       
       // Otherwise, emit a diagnostic and continue.
-      Diag(CurPtr, diag::ext_dollar_in_identifier);
+      if (!isLexingRawMode())
+        Diag(CurPtr, diag::ext_dollar_in_identifier);
       CurPtr = ConsumeChar(CurPtr, Size, Result);
       C = getCharAndSize(CurPtr, Size);
       continue;
@@ -594,7 +595,8 @@ void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
       C = getAndAdvanceChar(CurPtr, Result);
     } else if (C == '\n' || C == '\r' ||             // Newline.
                (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
-      if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
+      if (!isLexingRawMode())
+        Diag(BufferPtr, diag::err_unterminated_string);
       FormTokenWithChars(Result, CurPtr-1, tok::unknown);
       return;
     } else if (C == 0) {
@@ -604,7 +606,8 @@ void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
   }
   
   // If a nul character existed in the string, warn about it.
-  if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
+  if (NulCharacter && !isLexingRawMode())
+    Diag(NulCharacter, diag::null_in_string);
 
   // Update the location of the token as well as the BufferPtr instance var.
   FormTokenWithChars(Result, CurPtr,
@@ -624,7 +627,8 @@ void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
       C = getAndAdvanceChar(CurPtr, Result);
     } else if (C == '\n' || C == '\r' ||             // Newline.
                (C == 0 && CurPtr-1 == BufferEnd)) {  // End of file.
-      if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
+      if (!isLexingRawMode())
+        Diag(BufferPtr, diag::err_unterminated_string);
       FormTokenWithChars(Result, CurPtr-1, tok::unknown);
       return;
     } else if (C == 0) {
@@ -634,7 +638,8 @@ void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
   }
   
   // If a nul character existed in the string, warn about it.
-  if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
+  if (NulCharacter && !isLexingRawMode())
+    Diag(NulCharacter, diag::null_in_string);
   
   // Update the location of token as well as BufferPtr.
   FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
@@ -649,7 +654,8 @@ void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
   // Handle the common case of 'x' and '\y' efficiently.
   char C = getAndAdvanceChar(CurPtr, Result);
   if (C == '\'') {
-    if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
+    if (!isLexingRawMode())
+      Diag(BufferPtr, diag::err_empty_character);
     FormTokenWithChars(Result, CurPtr, tok::unknown);
     return;
   } else if (C == '\\') {
@@ -669,7 +675,8 @@ void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
         C = getAndAdvanceChar(CurPtr, Result);
       } else if (C == '\n' || C == '\r' ||               // Newline.
                  (C == 0 && CurPtr-1 == BufferEnd)) {    // End of file.
-        if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
+        if (!isLexingRawMode())
+          Diag(BufferPtr, diag::err_unterminated_char);
         FormTokenWithChars(Result, CurPtr-1, tok::unknown);
         return;
       } else if (C == 0) {
@@ -679,7 +686,8 @@ void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
     } while (C != '\'');
   }
   
-  if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
+  if (NulCharacter && !isLexingRawMode())
+    Diag(NulCharacter, diag::null_in_char);
 
   // Update the location of token as well as BufferPtr.
   FormTokenWithChars(Result, CurPtr, tok::char_constant);
@@ -738,7 +746,7 @@ bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
 bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
   // If BCPL comments aren't explicitly enabled for this language, emit an
   // extension warning.
-  if (!Features.BCPLComment) {
+  if (!Features.BCPLComment && !isLexingRawMode()) {
     Diag(BufferPtr, diag::ext_bcpl_comment);
     
     // Mark them enabled so we only emit one warning for this translation
@@ -788,7 +796,8 @@ bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
               break;
           }
           
-          Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
+          if (!isLexingRawMode())
+            Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
           break;
         }
     }
@@ -890,17 +899,21 @@ static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
     // If no trigraphs are enabled, warn that we ignored this trigraph and
     // ignore this * character.
     if (!L->getFeatures().Trigraphs) {
-      L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
+      if (!L->isLexingRawMode())
+        L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
       return false;
     }
-    L->Diag(CurPtr, diag::trigraph_ends_block_comment);
+    if (!L->isLexingRawMode())
+      L->Diag(CurPtr, diag::trigraph_ends_block_comment);
   }
   
   // Warn about having an escaped newline between the */ characters.
-  L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
+  if (!L->isLexingRawMode())
+    L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
   
   // If there was space between the backslash and newline, warn about it.
-  if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
+  if (HasSpace && !L->isLexingRawMode())
+    L->Diag(CurPtr, diag::backslash_newline_space);
   
   return true;
 }
@@ -934,7 +947,7 @@ bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
   unsigned char C = getCharAndSize(CurPtr, CharSize);
   CurPtr += CharSize;
   if (C == 0 && CurPtr == BufferEnd+1) {
-    if (!LexingRawMode)
+    if (!isLexingRawMode())
       Diag(BufferPtr, diag::err_unterminated_block_comment);
     --CurPtr;
     
@@ -1013,10 +1026,12 @@ bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
         // If this is a /* inside of the comment, emit a warning.  Don't do this
         // if this is a /*/, which will end the comment.  This misses cases with
         // embedded escaped newlines, but oh well.
-        Diag(CurPtr-1, diag::warn_nested_block_comment);
+        if (!isLexingRawMode())
+          Diag(CurPtr-1, diag::warn_nested_block_comment);
       }
     } else if (C == 0 && CurPtr == BufferEnd+1) {
-      if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_block_comment);
+      if (!isLexingRawMode())
+        Diag(BufferPtr, diag::err_unterminated_block_comment);
       // Note: the user probably forgot a */.  We could continue immediately
       // after the /*, but this would involve lexing a lot of what really is the
       // comment, which surely would confuse the parser.
@@ -1122,7 +1137,7 @@ bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
 
   // If we are in raw mode, return this event as an EOF token.  Let the caller
   // that put us in raw mode handle the event.
-  if (LexingRawMode) {
+  if (isLexingRawMode()) {
     Result.startToken();
     BufferPtr = BufferEnd;
     FormTokenWithChars(Result, BufferEnd, tok::eof);
@@ -1234,7 +1249,8 @@ LexNextToken:
       return PPCache->Lex(Result);
     }
     
-    Diag(CurPtr-1, diag::null_in_file);
+    if (!isLexingRawMode())
+      Diag(CurPtr-1, diag::null_in_file);
     Result.setFlag(Token::LeadingSpace);
     if (SkipWhitespace(Result, CurPtr))
       return; // KeepWhitespaceMode
@@ -1329,7 +1345,8 @@ LexNextToken:
 
   case '$':   // $ in identifiers.
     if (Features.DollarIdents) {
-      Diag(CurPtr-1, diag::ext_dollar_in_identifier);
+      if (!isLexingRawMode())
+        Diag(CurPtr-1, diag::ext_dollar_in_identifier);
       // Notify MIOpt that we read a non-whitespace/non-comment token.
       MIOpt.ReadToken();
       return LexIdentifier(Result, CurPtr);
@@ -1493,7 +1510,8 @@ LexNextToken:
                              SizeTmp2, Result);
       } else if (Char == '@' && Features.Microsoft) {  // %:@ -> #@ -> Charize
         CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
-        Diag(BufferPtr, diag::charize_microsoft_ext);
+        if (!isLexingRawMode())
+          Diag(BufferPtr, diag::charize_microsoft_ext);
         Kind = tok::hashat;
       } else {
         Kind = tok::hash;       // '%:' -> '#'
@@ -1623,7 +1641,8 @@ LexNextToken:
       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
     } else if (Char == '@' && Features.Microsoft) {  // #@ -> Charize
       Kind = tok::hashat;
-      Diag(BufferPtr, diag::charize_microsoft_ext);
+      if (!isLexingRawMode())
+        Diag(BufferPtr, diag::charize_microsoft_ext);
       CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
     } else {
       Kind = tok::hash;
index 1916f298dbda69e24c0319a725c96df180a09d3d..6e479625e0fde38ed13d5f139fa44f3a4c5daff8 100644 (file)
@@ -28,8 +28,6 @@ PreprocessorLexer::~PreprocessorLexer() {}
 
 void PreprocessorLexer::Diag(SourceLocation Loc, unsigned DiagID,
                              const std::string &Msg) const {
-  if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
-    return;
   PP->Diag(Loc, DiagID) << Msg;
 }