[cfe-commits] [PATCH] Fix crash printing diagnostic range spanning macros

Eli Friedman eli.friedman at gmail.com
Wed Oct 24 19:36:30 PDT 2012


Patch attached.  Fixes a crash on a testcase like the following:

+#define BAD_CONDITIONAL_OPERATOR (2<3)?4:5
+int x = BAD_CONDITIONAL_OPERATOR+BAD_CONDITIONAL_OPERATOR;

We try to print a source range which starts at the 5 in the first
expansion, and ends just after the 3 in the second expansion. This
causes an assertion failure in diagnostic emission because the
beginning of the range appears to be after the end.

My attempted fix is to try to suppress printing source ranges when the
beginning and end point into different macro expansions (even if the
physical locations happen to be on the same line).  I'm not confident
this is the correct fix, but it seems to be roughly on the right
track.  Any comments welcome.

(This is <rdar://problem/12472249>.)

-Eli
-------------- next part --------------
Index: lib/Frontend/DiagnosticRenderer.cpp
===================================================================
--- lib/Frontend/DiagnosticRenderer.cpp	(revision 166641)
+++ lib/Frontend/DiagnosticRenderer.cpp	(working copy)
@@ -285,10 +285,14 @@
        E = Ranges.end();
        I != E; ++I) {
     SourceLocation Start = I->getBegin(), End = I->getEnd();
-    if (Start.isMacroID())
+    if (Start.isMacroID() && End.isMacroID() &&
+        SM.getImmediateExpansionRange(Start).first ==
+        SM.getImmediateExpansionRange(End).first) {
       I->setBegin(SM.getImmediateMacroCalleeLoc(Start));
-    if (End.isMacroID())
       I->setEnd(SM.getImmediateMacroCalleeLoc(End));
+    } else if (Start.isMacroID() || End.isMacroID()) {
+      *I = CharSourceRange();
+    }
   }
   
   if (Suppressed) {
Index: test/Misc/caret-diags-macros.c
===================================================================
--- test/Misc/caret-diags-macros.c	(revision 166641)
+++ test/Misc/caret-diags-macros.c	(working copy)
@@ -118,3 +118,16 @@
   // CHECK: {{.*}}:104:70: note: expanded from macro 'variadic_pasting_args2a'
   // CHECK: {{.*}}:102:41: note: expanded from macro 'variadic_pasting_args1'
 }
+
+#define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
+int test4 = BAD_CONDITIONAL_OPERATOR+BAD_CONDITIONAL_OPERATOR;
+// CHECK: {{.*}}:122:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
+// CHECK: #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
+// CHECK:                                       ^
+// CHECK: {{.*}}:122:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
+// CHECK: #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
+// CHECK:                                       ^
+// CHECK: {{.*}}:122:39: note: expanded from macro 'BAD_CONDITIONAL_OPERATOR'
+// CHECK: #define BAD_CONDITIONAL_OPERATOR (2<3)?2:3
+// CHECK:                                  ~~~~~^~~~
+


More information about the cfe-commits mailing list