From: Richard Smith Date: Wed, 5 Dec 2012 11:04:55 +0000 (+0000) Subject: PR14049: Don't say "expanded from macro 'foo'" when 'foo' just happens to be X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5b10af7aa9e254a38d0a9f4d86caaccd61972124;p=clang PR14049: Don't say "expanded from macro 'foo'" when 'foo' just happens to be the LHS of a token paste. Use "expanded from here" instead when we're not sure it's actually a macro. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169373 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index 1bc940c940..89d3867aad 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -47,6 +47,11 @@ static StringRef getImmediateMacroName(SourceLocation Loc, while (SM.isMacroArgExpansion(Loc)) Loc = SM.getImmediateExpansionRange(Loc).first; + // If the macro's spelling has no FileID, then it's actually a token paste + // or stringization (or similar) and not a macro at all. + if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(Loc)))) + return StringRef(); + // 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. @@ -448,8 +453,11 @@ void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc, SmallString<100> MessageStorage; llvm::raw_svector_ostream Message(MessageStorage); - Message << "expanded from macro '" - << getImmediateMacroName(Loc, SM, LangOpts) << "'"; + StringRef MacroName = getImmediateMacroName(Loc, SM, LangOpts); + if (MacroName.empty()) + Message << "expanded from here"; + else + Message << "expanded from macro '" << MacroName << "'"; emitDiagnostic(SpellingLoc, DiagnosticsEngine::Note, Message.str(), SpellingRanges, ArrayRef(), &SM); diff --git a/test/Misc/diag-macro-backtrace.c b/test/Misc/diag-macro-backtrace.c index ea40cbec57..0d28d7b911 100644 --- a/test/Misc/diag-macro-backtrace.c +++ b/test/Misc/diag-macro-backtrace.c @@ -12,7 +12,7 @@ #define DROOL WAZ #define FOOL DROOL -FOOL +FOOL; // CHECK: :15:1: error: expected identifier or '(' // CHECK: FOOL @@ -50,4 +50,17 @@ FOOL // CHECK: :3:13: note: expanded from macro 'FOO' // CHECK: #define FOO 1+"hi" // CHECK: ^ -// CHECK: 1 error generated. + +#define ADD(a, b) a ## #b +ADD(L, foo) +// CHECK: error: expected identifier or '(' +// CHECK: ADD(L, foo) +// CHECK: {{^\^}} +// CHECK: note: expanded from macro 'ADD' +// CHECK: #define ADD(a, b) a ## #b +// CHECK: {{^ \^}} +// CHECK: note: expanded from here +// CHECK: L"foo" +// CHECK: {{^\^}} + +// CHECK: 2 errors generated. diff --git a/test/Modules/epic-fail.m b/test/Modules/epic-fail.m index 32ac52d89a..7c3faaf63f 100644 --- a/test/Modules/epic-fail.m +++ b/test/Modules/epic-fail.m @@ -6,7 +6,7 @@ // CHECK: While building module 'Module' imported from // CHECK: error: expected ';' after top level declarator -// CHECK: note: expanded from macro 'getModuleVersion' +// CHECK: note: expanded from here // CHECK: fatal error: could not build module 'Module' // CHECK: While building module 'DependsOnModule' imported from // CHECK: fatal error: could not build module 'Module'