]> granicus.if.org Git - clang/commitdiff
Add -cc1 option -fno-diagnostics-use-presumed-location, a handy mode for
authorRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 14 Nov 2012 23:55:25 +0000 (23:55 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Wed, 14 Nov 2012 23:55:25 +0000 (23:55 +0000)
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
include/clang/Basic/SourceManager.h
include/clang/Driver/CC1Options.td
lib/Basic/SourceManager.cpp
lib/Frontend/CompilerInvocation.cpp
lib/Frontend/DiagnosticRenderer.cpp
test/Misc/diag-presumed.c [new file with mode: 0644]

index 476ac1e373f054c57a5e4c8d038f373067dd4230..41bbff2edec25b4261a974173951b40659749a2b 100644 (file)
@@ -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.
index db6bfd2ad3ddb8030afa720894795453d7c1c8e8..5717816ff99efbbf79afe543b43d9829414e0daa 100644 (file)
@@ -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
index 61c3d4919fbef007d62b6171293a5c402897e115..be0653cb9c5be58578368375386b05b05fdb5214 100644 (file)
@@ -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<"<N>">,
   HelpText<"Set the tab stop distance.">;
 def ferror_limit : Separate<["-"], "ferror-limit">, MetaVarName<"<N>">,
index cd0284a18e5dea5e50f191fc7d29c48338f51f25..a32e41ef533b96a9430a2e6783e483dd831436f5 100644 (file)
@@ -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 =
index 31dcb23c49c7e37a443e25ea8a110efe33923d32..1da462abba78d2f20e4e89051bf598c874dc5069 100644 (file)
@@ -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);
index 359b82be60625d23457d84b347eb11341e642a33..2d156f702252f0ffb98307be9d5c022b7d664e97 100644 (file)
@@ -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 (file)
index 0000000..07b7cdf
--- /dev/null
@@ -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