This patch adds flags to print or omit the include stacks of notes.  These
stacks are less helpful in notes than those in errors and warnings.  Below
is the current output on an error, and how it will show up when the include
stack is disabled.

Current behavior:

test/test.cpp:9:10: error: no matching function for
      call to 'foo'
  return foo(1, 1);
         ^~~
In file included from test/test.cpp:7:
In file included from test/include7.h:1:
In file included from test/include6.h:1:
In file included from test/include5.h:1:
In file included from test/include4.h:1:
In file included from test/include3.h:1:
In file included from test/include2.h:1:
include1.h:1:5: note: candidate function not viable: requires 1
      argument, but 2 were provided
int foo(int x) { return x; }


With new flag to omit note include stacks:

test/test.cpp:9:10: error: no matching function for
      call to 'foo'
  return foo(1, 1);
         ^~~
include1.h:1:5: note: candidate function not viable: requires 1
      argument, but 2 were provided
int foo(int x) { return x; }
Index: clang/test/Misc/Inputs/include.h
===================================================================
--- clang/test/Misc/Inputs/include.h	(revision 0)
+++ clang/test/Misc/Inputs/include.h	(revision 0)
@@ -0,0 +1 @@
+int foo(int x) { return x; }
Index: clang/test/Misc/include-stack-for-note-flag.cpp
===================================================================
--- clang/test/Misc/include-stack-for-note-flag.cpp	(revision 0)
+++ clang/test/Misc/include-stack-for-note-flag.cpp	(revision 0)
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-show-note-include-stack %s 2>&1 | FileCheck %s -check-prefix=STACK
+// RUN: %clang_cc1 -fsyntax-only -fno-diagnostics-show-note-include-stack %s 2>&1 | FileCheck %s -check-prefix=STACKLESS
+// RUN: %clang_cc1 -fsyntax-only -fno-diagnostics-show-note-include-stack -fdiagnostics-show-note-include-stack %s 2>&1 | FileCheck %s -check-prefix=STACK
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-show-note-include-stack -fno-diagnostics-show-note-include-stack %s 2>&1 | FileCheck %s -check-prefix=STACKLESS
+// RUN: %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s -check-prefix=STACK
+
+#include "Inputs/include.h"
+int test() {
+  return foo(1, 1);
+}
+
+  // STACK: error: no matching function for call to 'foo'
+  // STACK:  In file included from
+  // STACK: note: candidate function not viable: requires 1 argument, but 2 were provided
+
+  // STACKLESS: error: no matching function for call to 'foo'
+  // STACKLESS-NOT:  In file included from
+  // STACKLESS: note: candidate function not viable: requires 1 argument, but 2 were provided
Index: clang/include/clang/Frontend/TextDiagnosticPrinter.h
===================================================================
--- clang/include/clang/Frontend/TextDiagnosticPrinter.h	(revision 127757)
+++ clang/include/clang/Frontend/TextDiagnosticPrinter.h	(working copy)
@@ -53,7 +53,8 @@
     LangOpts = 0;
   }
 
-  void PrintIncludeStack(SourceLocation Loc, const SourceManager &SM);
+  void PrintIncludeStack(Diagnostic::Level Level, SourceLocation Loc,
+                         const SourceManager &SM);
 
   void HighlightRange(const CharSourceRange &R,
                       const SourceManager &SrcMgr,
@@ -61,7 +62,7 @@
                       std::string &CaretLine,
                       const std::string &SourceLine);
 
-  void EmitCaretDiagnostic(SourceLocation Loc,
+  void EmitCaretDiagnostic(Diagnostic::Level Level, SourceLocation Loc,
                            CharSourceRange *Ranges, unsigned NumRanges,
                            const SourceManager &SM,
                            const FixItHint *Hints,
@@ -71,7 +72,7 @@
                            unsigned MacroSkipStart,
                            unsigned MacroSkipEnd);
 
-  virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+  virtual void HandleDiagnostic(Diagnostic::Level Level,
                                 const DiagnosticInfo &Info);
 };
 
Index: clang/include/clang/Frontend/DiagnosticOptions.h
===================================================================
--- clang/include/clang/Frontend/DiagnosticOptions.h	(revision 127757)
+++ clang/include/clang/Frontend/DiagnosticOptions.h	(working copy)
@@ -33,6 +33,7 @@
   unsigned ShowParseableFixits : 1; /// Show machine parseable fix-its.
   unsigned ShowOptionNames : 1;  /// Show the diagnostic name for mappable
                                  /// diagnostics.
+  unsigned ShowNoteIncludeStack : 1; /// Show include stacks for notes.
   unsigned ShowCategories : 2;   /// Show categories: 0 -> none, 1 -> Number,
                                  /// 2 -> Full Name.
   unsigned ShowColors : 1;       /// Show diagnostics with ANSI color sequences.
Index: clang/include/clang/Driver/CC1Options.td
===================================================================
--- clang/include/clang/Driver/CC1Options.td	(revision 127757)
+++ clang/include/clang/Driver/CC1Options.td	(working copy)
@@ -206,6 +206,9 @@
   HelpText<"Do not include source line and caret with diagnostics">;
 def fno_diagnostics_fixit_info : Flag<"-fno-diagnostics-fixit-info">,
   HelpText<"Do not include fixit information in diagnostics">;
+def fno_diagnostics_show_note_include_stack :
+  Flag<"-fno-diagnostics-show-note-include-stack">,
+  HelpText<"Display include stacks for diagnostic notes">;
 def w : Flag<"-w">, HelpText<"Suppress all warnings">;
 def pedantic : Flag<"-pedantic">;
 def pedantic_errors : Flag<"-pedantic-errors">;
@@ -223,7 +226,7 @@
   HelpText<"Print diagnostic name with mappable diagnostics">;
 def fdiagnostics_show_category : Separate<"-fdiagnostics-show-category">,
   HelpText<"Print diagnostic category">;
-
+def fdiagnostics_show_note_include_stack : Flag<"-fdiagnostics-show-note-include-stack">, HelpText<"Display include stacks for diagnostic notes">;
 def ftabstop : Separate<"-ftabstop">, MetaVarName<"<N>">,
   HelpText<"Set the tab stop distance.">;
 def ferror_limit : Separate<"-ferror-limit">, MetaVarName<"<N>">,
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td	(revision 127757)
+++ clang/include/clang/Driver/Options.td	(working copy)
@@ -271,6 +271,7 @@
 def fdiagnostics_print_source_range_info : Flag<"-fdiagnostics-print-source-range-info">, Group<f_Group>;
 def fdiagnostics_parseable_fixits : Flag<"-fdiagnostics-parseable-fixits">, Group<f_Group>;
 def fdiagnostics_show_option : Flag<"-fdiagnostics-show-option">, Group<f_Group>;
+def fdiagnostics_show_note_include_stack : Flag<"-fdiagnostics-show-note-include-stack">, Group<f_Group>;
 def fdiagnostics_show_category_EQ : Joined<"-fdiagnostics-show-category=">, Group<f_Group>;
 def fdollars_in_identifiers : Flag<"-fdollars-in-identifiers">, Group<f_Group>;
 def felide_constructors : Flag<"-felide-constructors">, Group<f_Group>;
@@ -329,6 +330,7 @@
 def fno_cxx_exceptions: Flag<"-fno-cxx-exceptions">, Group<f_Group>;
 def fno_diagnostics_fixit_info : Flag<"-fno-diagnostics-fixit-info">, Group<f_Group>;
 def fno_diagnostics_show_option : Flag<"-fno-diagnostics-show-option">, Group<f_Group>;
+def fno_diagnostics_show_note_include_stack : Flag<"-fno-diagnostics-show-note-include-stack">, Group<f_Group>;
 def fno_dollars_in_identifiers : Flag<"-fno-dollars-in-identifiers">, Group<f_Group>;
 def fno_elide_constructors : Flag<"-fno-elide-constructors">, Group<f_Group>;
 def fno_eliminate_unused_debug_symbols : Flag<"-fno-eliminate-unused-debug-symbols">, Group<f_Group>;
Index: clang/lib/Frontend/CompilerInvocation.cpp
===================================================================
--- clang/lib/Frontend/CompilerInvocation.cpp	(revision 127757)
+++ clang/lib/Frontend/CompilerInvocation.cpp	(working copy)
@@ -977,6 +977,13 @@
   Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location);
   Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
 
+  // Default behavior is to show note include stacks.
+  Opts.ShowNoteIncludeStack = true;
+  if (Arg *A = Args.getLastArg(OPT_fdiagnostics_show_note_include_stack,
+                               OPT_fno_diagnostics_show_note_include_stack))
+    if (A->getOption().matches(OPT_fno_diagnostics_show_note_include_stack))
+      Opts.ShowNoteIncludeStack = false;
+
   llvm::StringRef ShowOverloads =
     Args.getLastArgValue(OPT_fshow_overloads_EQ, "all");
   if (ShowOverloads == "best")
Index: clang/lib/Frontend/TextDiagnosticPrinter.cpp
===================================================================
--- clang/lib/Frontend/TextDiagnosticPrinter.cpp	(revision 127757)
+++ clang/lib/Frontend/TextDiagnosticPrinter.cpp	(working copy)
@@ -53,8 +53,11 @@
     delete &OS;
 }
 
-void TextDiagnosticPrinter::
-PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
+void TextDiagnosticPrinter::PrintIncludeStack(Diagnostic::Level Level,
+                                              SourceLocation Loc,
+                                              const SourceManager &SM) {
+  if (!DiagOpts->ShowNoteIncludeStack && Level == Diagnostic::Note) return;
+
   if (Loc.isInvalid()) return;
 
   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
@@ -62,7 +65,7 @@
     return;
   
   // Print out the other include frames first.
-  PrintIncludeStack(PLoc.getIncludeLoc(), SM);
+  PrintIncludeStack(Level, PLoc.getIncludeLoc(), SM);
 
   if (DiagOpts->ShowLocation)
     OS << "In file included from " << PLoc.getFilename()
@@ -289,7 +292,8 @@
   }
 }
 
-void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
+void TextDiagnosticPrinter::EmitCaretDiagnostic(Diagnostic::Level Level,
+                                                SourceLocation Loc,
                                                 CharSourceRange *Ranges,
                                                 unsigned NumRanges,
                                                 const SourceManager &SM,
@@ -313,7 +317,7 @@
 
     SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
     // FIXME: Map ranges?
-    EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns,
+    EmitCaretDiagnostic(Level, OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns,
                         OnMacroInst + 1, MacroSkipStart, MacroSkipEnd);
     
     // Map the location.
@@ -339,7 +343,7 @@
       // "included from" lines.
       if (LastWarningLoc != PLoc.getIncludeLoc()) {
         LastWarningLoc = PLoc.getIncludeLoc();
-        PrintIncludeStack(LastWarningLoc, SM);
+        PrintIncludeStack(Level, LastWarningLoc, SM);
       }
 
       if (DiagOpts->ShowLocation) {
@@ -351,8 +355,9 @@
       }
       OS << "note: instantiated from:\n";
       
-      EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns,
-                          OnMacroInst + 1, MacroSkipStart, MacroSkipEnd);
+      EmitCaretDiagnostic(Level, Loc, Ranges, NumRanges, SM, Hints, NumHints,
+                          Columns, OnMacroInst + 1, MacroSkipStart,
+                          MacroSkipEnd);
       return;
     }
     
@@ -805,7 +810,7 @@
       // "included from" lines.
       if (LastWarningLoc != PLoc.getIncludeLoc()) {
         LastWarningLoc = PLoc.getIncludeLoc();
-        PrintIncludeStack(LastWarningLoc, SM);
+        PrintIncludeStack(Level, LastWarningLoc, SM);
         StartOfLocationInfo = OS.tell();
       }
 
@@ -1034,7 +1039,7 @@
       }
     }        
     
-    EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
+    EmitCaretDiagnostic(Level, LastLoc, Ranges, NumRanges, LastLoc.getManager(),
                         Info.getFixItHints(),
                         Info.getNumFixItHints(),
                         DiagOpts->MessageLength, 
Index: clang/lib/Driver/Tools.cpp
===================================================================
--- clang/lib/Driver/Tools.cpp	(revision 127757)
+++ clang/lib/Driver/Tools.cpp	(working copy)
@@ -1704,6 +1704,16 @@
     CmdArgs.push_back(A->getValue(Args));
   }
 
+  if (Arg *A = Args.getLastArg(
+      options::OPT_fdiagnostics_show_note_include_stack,
+      options::OPT_fno_diagnostics_show_note_include_stack)) {
+    if (A->getOption().matches(
+        options::OPT_fdiagnostics_show_note_include_stack))
+      CmdArgs.push_back("-fdiagnostics-show-note-include-stack");
+    else
+      CmdArgs.push_back("-fno-diagnostics-show-note-include-stack");
+  }
+
   // Color diagnostics are the default, unless the terminal doesn't support
   // them.
   if (Args.hasFlag(options::OPT_fcolor_diagnostics,
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to