From 62221b17c90457df9ca0ff20bb54d634e8951def Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 14 Nov 2012 23:55:25 +0000 Subject: [PATCH] Add -cc1 option -fno-diagnostics-use-presumed-location, a handy mode for working with preprocessed testcases. This causes source locations in diagnostics to point at the spelling location instead of the presumed location, while still keeping the semantic effects of the line directives (entering and leaving system-header mode, primarily). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@168004 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticOptions.def | 1 + include/clang/Basic/SourceManager.h | 8 +++-- include/clang/Driver/CC1Options.td | 2 ++ lib/Basic/SourceManager.cpp | 5 ++-- lib/Frontend/CompilerInvocation.cpp | 1 + lib/Frontend/DiagnosticRenderer.cpp | 4 +-- test/Misc/diag-presumed.c | 36 +++++++++++++++++++++++ 7 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 test/Misc/diag-presumed.c diff --git a/include/clang/Basic/DiagnosticOptions.def b/include/clang/Basic/DiagnosticOptions.def index 476ac1e373..41bbff2ede 100644 --- a/include/clang/Basic/DiagnosticOptions.def +++ b/include/clang/Basic/DiagnosticOptions.def @@ -54,6 +54,7 @@ DIAGOPT(ShowCarets, 1, 1) /// Show carets in diagnostics. DIAGOPT(ShowFixits, 1, 1) /// Show fixit information. DIAGOPT(ShowSourceRanges, 1, 0) /// Show source ranges in numeric form. DIAGOPT(ShowParseableFixits, 1, 0) /// Show machine parseable fix-its. +DIAGOPT(ShowPresumedLoc, 1, 0) /// Show presumed location for diagnostics. DIAGOPT(ShowOptionNames, 1, 0) /// Show the option name for mappable /// diagnostics. DIAGOPT(ShowNoteIncludeStack, 1, 0) /// Show include stacks for notes. diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index db6bfd2ad3..5717816ff9 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -1187,7 +1187,8 @@ public: /// presumed location cannot be calculate (e.g., because \p Loc is invalid /// or the file containing \p Loc has changed on disk), returns an invalid /// presumed location. - PresumedLoc getPresumedLoc(SourceLocation Loc) const; + PresumedLoc getPresumedLoc(SourceLocation Loc, + bool UseLineDirectives = true) const; /// \brief Returns true if both SourceLocations correspond to the same file. bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { @@ -1423,7 +1424,8 @@ public: /// Get a presumed location suitable for displaying in a diagnostic message, /// taking into account macro arguments and expansions. - PresumedLoc getPresumedLocForDisplay(SourceLocation Loc) const { + PresumedLoc getPresumedLocForDisplay(SourceLocation Loc, + bool UseLineDirectives = true) const{ // This is a condensed form of the algorithm used by emitCaretDiagnostic to // walk to the top of the macro call stack. while (Loc.isMacroID()) { @@ -1431,7 +1433,7 @@ public: Loc = getImmediateMacroCallerLoc(Loc); } - return getPresumedLoc(Loc); + return getPresumedLoc(Loc, UseLineDirectives); } /// Look through spelling locations for a macro argument expansion, and if diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index 61c3d4919f..be0653cb9c 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -230,6 +230,8 @@ def fdiagnostics_format : Separate<["-"], "fdiagnostics-format">, HelpText<"Change diagnostic formatting to match IDE and command line tools">; def fdiagnostics_show_category : Separate<["-"], "fdiagnostics-show-category">, HelpText<"Print diagnostic category">; +def fno_diagnostics_use_presumed_location : Flag<["-"], "fno-diagnostics-use-presumed-location">, + HelpText<"Ignore #line directives when displaying diagnostic locations">; def ftabstop : Separate<["-"], "ftabstop">, MetaVarName<"">, HelpText<"Set the tab stop distance.">; def ferror_limit : Separate<["-"], "ferror-limit">, MetaVarName<"">, diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index cd0284a18e..a32e41ef53 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -1361,7 +1361,8 @@ const char *SourceManager::getBufferName(SourceLocation Loc, /// /// Note that a presumed location is always given as the expansion point of an /// expansion location, not at the spelling location. -PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { +PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, + bool UseLineDirectives) const { if (Loc.isInvalid()) return PresumedLoc(); // Presumed locations are always for expansion points. @@ -1395,7 +1396,7 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { // If we have #line directives in this file, update and overwrite the physical // location info if appropriate. - if (FI.hasLineDirectives()) { + if (UseLineDirectives && FI.hasLineDirectives()) { assert(LineTable && "Can't have linetable entries without a LineTable!"); // See if there is a #line directive before this. If so, get it. if (const LineEntry *Entry = diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 31dcb23c49..1da462abba 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -538,6 +538,7 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info); Opts.ShowParseableFixits = Args.hasArg(OPT_fdiagnostics_parseable_fixits); + Opts.ShowPresumedLoc = !Args.hasArg(OPT_fno_diagnostics_use_presumed_location); Opts.VerifyDiagnostics = Args.hasArg(OPT_verify); Opts.ElideType = !Args.hasArg(OPT_fno_elide_type); Opts.ShowTemplateTree = Args.hasArg(OPT_fdiagnostics_show_template_tree); diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index 359b82be60..2d156f7022 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -128,7 +128,7 @@ void DiagnosticRenderer::emitDiagnostic(SourceLocation Loc, PresumedLoc PLoc; if (Loc.isValid()) { - PLoc = SM->getPresumedLocForDisplay(Loc); + PLoc = SM->getPresumedLocForDisplay(Loc, DiagOpts->ShowPresumedLoc); // First, if this diagnostic is not in the main file, print out the // "included from" lines. @@ -208,7 +208,7 @@ void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc, if (Loc.isInvalid()) return; - PresumedLoc PLoc = SM.getPresumedLoc(Loc); + PresumedLoc PLoc = SM.getPresumedLoc(Loc, DiagOpts->ShowPresumedLoc); if (PLoc.isInvalid()) return; diff --git a/test/Misc/diag-presumed.c b/test/Misc/diag-presumed.c new file mode 100644 index 0000000000..07b7cdfe35 --- /dev/null +++ b/test/Misc/diag-presumed.c @@ -0,0 +1,36 @@ +// RUN: %clang_cc1 -fsyntax-only -pedantic-errors %s 2>&1 | FileCheck %s --check-prefix=PRESUMED +// RUN: %clang_cc1 -fsyntax-only -pedantic-errors -fno-diagnostics-use-presumed-location %s 2>&1 | FileCheck %s --check-prefix=SPELLING + +#line 100 +#define X(y) y +X(int n = error); + +// PRESUMED: diag-presumed.c:101:11: error: use of undeclared identifier 'error' +// PRESUMED: diag-presumed.c:100:14: note: expanded from +// SPELLING: diag-presumed.c:6:11: error: use of undeclared identifier 'error' +// SPELLING: diag-presumed.c:5:14: note: expanded from + +; +// PRESUMED: diag-presumed.c:108:1: error: extra ';' outside of a functio +// SPELLING: diag-presumed.c:13:1: error: extra ';' outside of a functio + +# 1 "thing1.cc" 1 +# 1 "thing1.h" 1 +# 1 "systemheader.h" 1 3 +; +// No diagnostic here: we're in a system header, even if we're using spelling +// locations for the diagnostics.. +// PRESUMED-NOT: extra ';' +// SPELLING-NOT: extra ';' + +another error; +// PRESUMED: included from {{.*}}diag-presumed.c:112: +// PRESUMED: from thing1.cc:1: +// PRESUMED: from thing1.h:1: +// PRESUMED: systemheader.h:7:1: error: unknown type name 'another' + +// SPELLING-NOT: included from +// SPELLING: diag-presumed.c:26:1: error: unknown type name 'another' + +# 1 "thing1.h" 2 +# 1 "thing1.cc" 2 -- 2.40.0