[cfe-commits] r142127 - in /cfe/trunk: lib/Frontend/TextDiagnostic.cpp test/Misc/caret-diags-macros.c test/Misc/include-stack-for-note-flag.cpp test/Misc/macro-backtrace.c

Chandler Carruth chandlerc at gmail.com
Sun Oct 16 02:30:08 PDT 2011


Author: chandlerc
Date: Sun Oct 16 04:30:08 2011
New Revision: 142127

URL: http://llvm.org/viewvc/llvm-project?rev=142127&view=rev
Log:
Now that macro expansion notes are real notes and go through the same
formatting as any other diagnostic, they will be properly line wrapped and
otherwise pretty printed. Let's take advantage of that and the new factoring to
add some helpful information to them (much like template backtrace notes and
other notes): the name of the macro whose expansion is being noted. This makes
a world of difference if caret diagnostics are disabled, making the expansion
notes actually useful in this case. It also helps ensure that in edge cases the
information the user needs is present. Consider:

% nl -ba t5.cc
     1  #define M(x, y, z) \
     2    y
     3
     4  M(
     5    1,
     6    2,
     7    3);

We now produce:
% ./bin/clang -fsyntax-only t5.cc
t5.cc:6:3: error: expected unqualified-id
  2,
  ^
t5.cc:2:3: note: expanded from macro: M
  y
  ^
1 error generated.

Without the added information in the note, the name of the macro being expanded
would never be shown.

This also deletes a FIXME to use the diagnostic formatting. It's not yet clear
to me that we *can* do this reasonably, and the production of this message was
my primary goal here anyways.

I'd love any comments or suggestions on improving these notes, their wording,
etc. Currently, I need to make them provide more helpful information in the
presence of a token-pasting buffer, and I'm pondering adding something along
the lines of "expanded from argument N of macro: ...".

Modified:
    cfe/trunk/lib/Frontend/TextDiagnostic.cpp
    cfe/trunk/test/Misc/caret-diags-macros.c
    cfe/trunk/test/Misc/include-stack-for-note-flag.cpp
    cfe/trunk/test/Misc/macro-backtrace.c

Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=142127&r1=142126&r2=142127&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Sun Oct 16 04:30:08 2011
@@ -689,6 +689,9 @@
   emitMacroExpansionsAndCarets(OneLevelUp, Level, Ranges, Hints, MacroDepth,
                                OnMacroInst + 1);
 
+  // Save the original location so we can find the spelling of the macro call.
+  SourceLocation MacroLoc = Loc;
+
   // Map the location.
   Loc = getImmediateMacroCalleeLoc(SM, Loc);
 
@@ -726,9 +729,27 @@
     return;
   }
 
-  // FIXME: Format an actual diagnostic rather than a hard coded string.
+  // Walk past macro argument expanions.
+  while (SM.isMacroArgExpansion(MacroLoc))
+    MacroLoc = SM.getImmediateExpansionRange(MacroLoc).first;
+
+  // Find the spelling location of the start of the non-argument expansion
+  // range. This is where the macro name was spelled in order to begin
+  // expanding this macro.
+  MacroLoc = SM.getSpellingLoc(SM.getImmediateExpansionRange(MacroLoc).first);
+
+  // Dig out the buffer where the macro name was spelled and the extents of the
+  // name so that we can render it into the expansion note.
+  std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(MacroLoc);
+  unsigned MacroTokenLength = Lexer::MeasureTokenLength(MacroLoc, SM, LangOpts);
+  StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
+
+  llvm::SmallString<100> MessageStorage;
+  llvm::raw_svector_ostream Message(MessageStorage);
+  Message << "expanded from macro: "
+          << ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
   emitDiagnostic(SM.getSpellingLoc(Loc), DiagnosticsEngine::Note,
-                 "expanded from:",
+                 Message.str(),
                  Ranges, ArrayRef<FixItHint>());
 }
 

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=142127&r1=142126&r2=142127&view=diff
==============================================================================
--- cfe/trunk/test/Misc/caret-diags-macros.c (original)
+++ cfe/trunk/test/Misc/caret-diags-macros.c Sun Oct 16 04:30:08 2011
@@ -6,8 +6,8 @@
   M1(
     M2);
   // CHECK: :7:{{[0-9]+}}: warning: expression result unused
-  // CHECK: :4:{{[0-9]+}}: note: expanded from:
-  // CHECK: :3:{{[0-9]+}}: note: expanded from:
+  // CHECK: :4:{{[0-9]+}}: note: expanded from macro: M2
+  // CHECK: :3:{{[0-9]+}}: note: expanded from macro: M1
 }
 
 #define A 1
@@ -16,9 +16,9 @@
 void bar() {
   C;
   // CHECK: :17:3: warning: expression result unused
-  // CHECK: :15:11: note: expanded from:
-  // CHECK: :14:11: note: expanded from:
-  // CHECK: :13:11: note: expanded from:
+  // CHECK: :15:11: note: expanded from macro: C
+  // CHECK: :14:11: note: expanded from macro: B
+  // CHECK: :13:11: note: expanded from macro: A
 }
 
 // rdar://7597492
@@ -46,28 +46,28 @@
   // its easy to FileCheck.
   // CHECK-NEXT: macro_args3(1);
   // CHECK-NEXT: ~~~~~~~~~~~~^~
-  // CHECK: {{.*}}:36:36: note: expanded from:
-  // CHECK: {{.*}}:35:36: note: expanded from:
-  // CHECK: {{.*}}:34:24: note: expanded from:
+  // CHECK: {{.*}}:36:36: note: expanded from macro: macro_args3
+  // CHECK: {{.*}}:35:36: note: expanded from macro: macro_args2
+  // CHECK: {{.*}}:34:24: note: expanded from macro: macro_args1
 
   macro_many_args3(
     1,
     2,
     3);
   // CHECK: {{.*}}:55:5: warning: expression result unused
-  // CHECK: {{.*}}:40:55: note: expanded from:
-  // CHECK: {{.*}}:39:55: note: expanded from:
-  // CHECK: {{.*}}:38:35: note: expanded from:
+  // CHECK: {{.*}}:40:55: note: expanded from macro: macro_many_args3
+  // CHECK: {{.*}}:39:55: note: expanded from macro: macro_many_args2
+  // CHECK: {{.*}}:38:35: note: expanded from macro: macro_many_args1
 
   macro_many_args3(
     1,
     M2,
     3);
   // CHECK: {{.*}}:64:5: warning: expression result unused
-  // CHECK: {{.*}}:4:12: note: expanded from:
-  // CHECK: {{.*}}:40:55: note: expanded from:
-  // CHECK: {{.*}}:39:55: note: expanded from:
-  // CHECK: {{.*}}:38:35: note: expanded from:
+  // CHECK: {{.*}}:4:12: note: expanded from macro: M2
+  // CHECK: {{.*}}:40:55: note: expanded from macro: macro_many_args3
+  // CHECK: {{.*}}:39:55: note: expanded from macro: macro_many_args2
+  // CHECK: {{.*}}:38:35: note: expanded from macro: macro_many_args1
 
   macro_many_args3(
     1,
@@ -78,11 +78,11 @@
   // arguments.
   // CHECK-NEXT: macro_args2(2),
   // CHECK-NEXT: ~~~~~~~~~~~~^~~
-  // CHECK: {{.*}}:35:36: note: expanded from:
-  // CHECK: {{.*}}:34:24: note: expanded from:
-  // CHECK: {{.*}}:40:55: note: expanded from:
-  // CHECK: {{.*}}:39:55: note: expanded from:
-  // CHECK: {{.*}}:38:35: note: expanded from:
+  // CHECK: {{.*}}:35:36: note: expanded from macro: macro_args2
+  // CHECK: {{.*}}:34:24: note: expanded from macro: macro_args1
+  // CHECK: {{.*}}:40:55: note: expanded from macro: macro_many_args3
+  // CHECK: {{.*}}:39:55: note: expanded from macro: macro_many_args2
+  // CHECK: {{.*}}:38:35: note: expanded from macro: macro_many_args1
 }
 
 #define variadic_args1(x, y, ...) y
@@ -94,9 +94,9 @@
   // CHECK: {{.*}}:93:21: warning: expression result unused
   // CHECK-NEXT: variadic_args3(1, 2, 3, 4);
   // CHECK-NEXT: ~~~~~~~~~~~~~~~~~~^~~~~~~~
-  // CHECK: {{.*}}:90:53: note: expanded from:
-  // CHECK: {{.*}}:89:50: note: expanded from:
-  // CHECK: {{.*}}:88:35: note: expanded from:
+  // CHECK: {{.*}}:90:53: note: expanded from macro: variadic_args3
+  // CHECK: {{.*}}:89:50: note: expanded from macro: variadic_args2
+  // CHECK: {{.*}}:88:35: note: expanded from macro: variadic_args1
 }
 
 #define variadic_pasting_args1(x, y, z) y
@@ -108,13 +108,13 @@
 void test3() {
   variadic_pasting_args3(1, 2, 3, 4);
   // CHECK: {{.*}}:109:32: warning: expression result unused
-  // CHECK: {{.*}}:105:72: note: expanded from:
-  // CHECK: {{.*}}:103:68: note: expanded from:
-  // CHECK: {{.*}}:102:41: note: expanded from:
+  // CHECK: {{.*}}:105:72: note: expanded from macro: variadic_pasting_args3
+  // CHECK: {{.*}}:103:68: note: expanded from macro: variadic_pasting_args2
+  // CHECK: {{.*}}:102:41: note: expanded from macro: variadic_pasting_args1
 
   variadic_pasting_args3a(1, 2, 3, 4);
   // CHECK: {{.*}}:115:30: warning: expression result unused
-  // CHECK: {{.*}}:106:71: note: expanded from:
-  // CHECK: {{.*}}:104:70: note: expanded from:
-  // CHECK: {{.*}}:102:41: note: expanded from:
+  // CHECK: {{.*}}:106:71: note: expanded from macro: variadic_pasting_args3a
+  // CHECK: {{.*}}:104:70: note: expanded from macro: variadic_pasting_args2a
+  // CHECK: {{.*}}:102:41: note: expanded from macro: variadic_pasting_args1
 }

Modified: cfe/trunk/test/Misc/include-stack-for-note-flag.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/include-stack-for-note-flag.cpp?rev=142127&r1=142126&r2=142127&view=diff
==============================================================================
--- cfe/trunk/test/Misc/include-stack-for-note-flag.cpp (original)
+++ cfe/trunk/test/Misc/include-stack-for-note-flag.cpp Sun Oct 16 04:30:08 2011
@@ -18,11 +18,11 @@
 // STACK: note: candidate function not viable
 // STACK: error: comparison between pointer and integer
 // STACK:  In file included from
-// STACK: note: expanded from:
+// STACK: note: expanded from macro:
 
 // STACKLESS: error: no matching function for call to 'foo'
 // STACKLESS-NOT:  In file included from
 // STACKLESS: note: candidate function not viable
 // STACKLESS: error: comparison between pointer and integer
 // STACKLESS-NOT:  In file included from
-// STACKLESS: note: expanded from:
+// STACKLESS: note: expanded from macro:

Modified: cfe/trunk/test/Misc/macro-backtrace.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Misc/macro-backtrace.c?rev=142127&r1=142126&r2=142127&view=diff
==============================================================================
--- cfe/trunk/test/Misc/macro-backtrace.c (original)
+++ cfe/trunk/test/Misc/macro-backtrace.c Sun Oct 16 04:30:08 2011
@@ -20,16 +20,16 @@
   // RUN:   | FileCheck %s -check-prefix=CHECK-LIMIT
   // CHECK-LIMIT: macro-backtrace.c:18:7: warning: comparison of distinct pointer types ('int *' and 'float *')
   // CHECK-LIMIT: if (M12(ip, fp)) { }
-  // CHECK-LIMIT: macro-backtrace.c:15:19: note: expanded from:
+  // CHECK-LIMIT: macro-backtrace.c:15:19: note: expanded from macro: M12
   // CHECK-LIMIT: #define M12(A, B) M11(A, B)
-  // CHECK-LIMIT: macro-backtrace.c:14:19: note: expanded from:
+  // CHECK-LIMIT: macro-backtrace.c:14:19: note: expanded from macro: M11
   // CHECK-LIMIT: #define M11(A, B) M10(A, B)
   // CHECK-LIMIT: note: (skipping 7 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
-  // CHECK-LIMIT: macro-backtrace.c:6:18: note: expanded from:
+  // CHECK-LIMIT: macro-backtrace.c:6:18: note: expanded from macro: M3
   // CHECK-LIMIT: #define M3(A, B) M2(A, B)
-  // CHECK-LIMIT: macro-backtrace.c:5:18: note: expanded from:
+  // CHECK-LIMIT: macro-backtrace.c:5:18: note: expanded from macro: M2
   // CHECK-LIMIT: #define M2(A, B) M1(A, B)
-  // CHECK-LIMIT: macro-backtrace.c:4:23: note: expanded from:
+  // CHECK-LIMIT: macro-backtrace.c:4:23: note: expanded from macro: M1
   // CHECK-LIMIT: #define M1(A, B) ((A) < (B))
 
   // FIXME: We should have higher quality messages, especially when caret
@@ -37,11 +37,11 @@
   // RUN: %clang_cc1 -fsyntax-only -fno-caret-diagnostics %s 2>&1 \
   // RUN:   | FileCheck %s -check-prefix=CHECK-NO-CARETS
   // CHECK-NO-CARETS: macro-backtrace.c:18:7: warning: comparison of distinct pointer types ('int *' and 'float *')
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:15:19: note: expanded from:
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:14:19: note: expanded from:
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:13:19: note: expanded from:
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:15:19: note: expanded from macro: M12
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:14:19: note: expanded from macro: M11
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:13:19: note: expanded from macro: M10
   // CHECK-NO-CARETS-NEXT: note: (skipping 6 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:6:18: note: expanded from:
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:5:18: note: expanded from:
-  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:4:23: note: expanded from:
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:6:18: note: expanded from macro: M3
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:5:18: note: expanded from macro: M2
+  // CHECK-NO-CARETS-NEXT: macro-backtrace.c:4:23: note: expanded from macro: M1
 }





More information about the cfe-commits mailing list