[cfe-commits] r168984 - in /cfe/trunk: lib/Frontend/DiagnosticRenderer.cpp test/Misc/caret-diags-macros.c

Eli Friedman eli.friedman at gmail.com
Thu Nov 29 22:19:41 PST 2012


Author: efriedma
Date: Fri Nov 30 00:19:40 2012
New Revision: 168984

URL: http://llvm.org/viewvc/llvm-project?rev=168984&view=rev
Log:
Fix the computation of highlight ranges so we produce something sane when
the beginning and end of the range are in different macro arguments.
PR14399.


Modified:
    cfe/trunk/lib/Frontend/DiagnosticRenderer.cpp
    cfe/trunk/test/Misc/caret-diags-macros.c

Modified: cfe/trunk/lib/Frontend/DiagnosticRenderer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/DiagnosticRenderer.cpp?rev=168984&r1=168983&r2=168984&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/DiagnosticRenderer.cpp (original)
+++ cfe/trunk/lib/Frontend/DiagnosticRenderer.cpp Fri Nov 30 00:19:40 2012
@@ -242,20 +242,40 @@
     SourceLocation Begin = I->getBegin(), End = I->getEnd();
     bool IsTokenRange = I->isTokenRange();
 
-    // Search the macro caller chain for the beginning of the range.
-    while (Begin.isMacroID() && SM->getFileID(Begin) != CaretLocFileID)
-      Begin = SM->getImmediateMacroCallerLoc(Begin);
-
-    // Search the macro caller chain for the beginning of the range.
-    while (End.isMacroID() && SM->getFileID(End) != CaretLocFileID) {
-      // The computation of the next End is an inlined version of
-      // getImmediateMacroCallerLoc, except it chooses the end of an
-      // expansion range.
-      if (SM->isMacroArgExpansion(End)) {
+    FileID BeginFileID = SM->getFileID(Begin);
+    FileID EndFileID = SM->getFileID(End);
+
+    // Find the common parent for the beginning and end of the range.
+
+    // First, crawl the expansion chain for the beginning of the range.
+    llvm::SmallDenseMap<FileID, SourceLocation> BeginLocsMap;
+    while (Begin.isMacroID() && BeginFileID != EndFileID) {
+      BeginLocsMap[BeginFileID] = Begin;
+      Begin = SM->getImmediateExpansionRange(Begin).first;
+      BeginFileID = SM->getFileID(Begin);
+    }
+
+    // Then, crawl the expansion chain for the end of the range.
+    if (BeginFileID != EndFileID) {
+      while (End.isMacroID() && !BeginLocsMap.count(EndFileID)) {
+        End = SM->getImmediateExpansionRange(End).second;
+        EndFileID = SM->getFileID(End);
+      }
+      if (End.isMacroID()) {
+        Begin = BeginLocsMap[EndFileID];
+        BeginFileID = EndFileID;
+      }
+    }
+
+    while (Begin.isMacroID() && BeginFileID != CaretLocFileID) {
+      if (SM->isMacroArgExpansion(Begin)) {
+        Begin = SM->getImmediateSpellingLoc(Begin);
         End = SM->getImmediateSpellingLoc(End);
       } else {
+        Begin = SM->getImmediateExpansionRange(Begin).first;
         End = SM->getImmediateExpansionRange(End).second;
       }
+      BeginFileID = SM->getFileID(Begin);
     }
 
     // Return the spelling location of the beginning and end of the range.

Modified: cfe/trunk/test/Misc/caret-diags-macros.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/caret-diags-macros.c?rev=168984&r1=168983&r2=168984&view=diff
==============================================================================
--- cfe/trunk/test/Misc/caret-diags-macros.c (original)
+++ cfe/trunk/test/Misc/caret-diags-macros.c Fri Nov 30 00:19:40 2012
@@ -163,3 +163,17 @@
 // CHECK-NEXT:    {{.*}}:134:15: note: expanded from macro 'QMARK'
 // CHECK-NEXT:    #define QMARK ?
 // CHECK-NEXT: {{^              \^}}
+
+// PR14399
+void iequals(int,int,int);
+void foo_aa()
+{
+#define /* */ BARC(c, /* */b, a, ...) (a+b+/* */c + __VA_ARGS__ +0)
+  iequals(__LINE__, BARC(4,3,2,6,8), 8);
+}
+// CHECK:         {{.*}}:172:21: warning: expression result unused
+// CHECK-NEXT:      iequals(__LINE__, BARC(4,3,2,6,8), 8);
+// CHECK-NEXT: {{^                    \^~~~~~~~~~~~~~~}}
+// CHECK-NEXT:    {{.*}}:171:51: note: expanded from macro 'BARC'
+// CHECK-NEXT:    #define /* */ BARC(c, /* */b, a, ...) (a+b+/* */c + __VA_ARGS__ +0)
+// CHECK-NEXT: {{^                                       ~~~~~~~~~~ \^}}





More information about the cfe-commits mailing list