[cfe-commits] r104569 - /cfe/trunk/lib/Parse/ParseExpr.cpp

Chris Lattner sabre at nondot.org
Mon May 24 15:31:37 PDT 2010


Author: lattner
Date: Mon May 24 17:31:37 2010
New Revision: 104569

URL: http://llvm.org/viewvc/llvm-project?rev=104569&view=rev
Log:
improve the fixit for the missing : error when parsing ?:.  When
there are already two spaces before the token where the : was expected,
put the : in between the spaces.  This means we get it right in both
of these cases:

t.c:2:17: error: expected ':'
  return a ? b  c;
                ^
               :
t.c:3:16: error: expected ':'
  return a ? b c;
               ^
               : 

In the later case, the diagnostic says to insert ": ", in the former
case it says to insert ":" between the spaces.  This fixes rdar://8007231

Modified:
    cfe/trunk/lib/Parse/ParseExpr.cpp

Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=104569&r1=104568&r2=104569&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Mon May 24 17:31:37 2010
@@ -315,8 +315,29 @@
         // Eat the colon.
         ColonLoc = ConsumeToken();
       } else {
+        // Otherwise, we're missing a ':'.  Assume that this was a typo that the
+        // user forgot.  If we're not in a macro instantion, we can suggest a
+        // fixit hint.  If there were two spaces before the current token,
+        // suggest inserting the colon in between them, otherwise insert ": ".
+        SourceLocation FILoc = Tok.getLocation();
+        const char *FIText = ": ";
+        if (FILoc.isFileID()) {
+          const SourceManager &SM = PP.getSourceManager();
+          bool IsInvalid = false;
+          const char *SourcePtr =
+            SM.getCharacterData(FILoc.getFileLocWithOffset(-1), &IsInvalid);
+          if (!IsInvalid && *SourcePtr == ' ') {
+            SourcePtr =
+              SM.getCharacterData(FILoc.getFileLocWithOffset(-2), &IsInvalid);
+            if (!IsInvalid && *SourcePtr == ' ') {
+              FILoc = FILoc.getFileLocWithOffset(-1);
+              FIText = ":";
+            }
+          }
+        }
+        
         Diag(Tok, diag::err_expected_colon)
-          << FixItHint::CreateInsertion(Tok.getLocation(), ": ");
+          << FixItHint::CreateInsertion(FILoc, FIText);
         Diag(OpToken, diag::note_matching) << "?";
         ColonLoc = Tok.getLocation();
       }





More information about the cfe-commits mailing list