]> granicus.if.org Git - clang/commitdiff
Sink all of the include stack printing logic into its member function.
authorChandler Carruth <chandlerc@gmail.com>
Wed, 31 Aug 2011 23:59:19 +0000 (23:59 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Wed, 31 Aug 2011 23:59:19 +0000 (23:59 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138920 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/TextDiagnosticPrinter.cpp

index c531ddd846134a2e7baa3c9990944b7873a18ab8..ad466c4e49d8a0dfa538a8db442147aa1c21a5b0 100644 (file)
@@ -53,27 +53,54 @@ TextDiagnosticPrinter::~TextDiagnosticPrinter() {
     delete &OS;
 }
 
-void TextDiagnosticPrinter::PrintIncludeStack(Diagnostic::Level Level,
-                                              SourceLocation Loc,
-                                              const SourceManager &SM) {
-  if (!DiagOpts->ShowNoteIncludeStack && Level == Diagnostic::Note) return;
-
-  if (Loc.isInvalid()) return;
+/// \brief Helper to recursivly walk up the include stack and print each layer
+/// on the way back down.
+static void PrintIncludeStackRecursively(raw_ostream &OS,
+                                         const SourceManager &SM,
+                                         SourceLocation Loc,
+                                         bool ShowLocation) {
+  if (Loc.isInvalid())
+    return;
 
   PresumedLoc PLoc = SM.getPresumedLoc(Loc);
   if (PLoc.isInvalid())
     return;
-  
+
   // Print out the other include frames first.
-  PrintIncludeStack(Level, PLoc.getIncludeLoc(), SM);
+  PrintIncludeStackRecursively(OS, SM, PLoc.getIncludeLoc(), ShowLocation);
 
-  if (DiagOpts->ShowLocation)
+  if (ShowLocation)
     OS << "In file included from " << PLoc.getFilename()
        << ':' << PLoc.getLine() << ":\n";
   else
     OS << "In included file:\n";
 }
 
+/// \brief Prints an include stack when appropriate for a particular diagnostic
+/// level and location.
+///
+/// This routine handles all the logic of suppressing particular include stacks
+/// (such as those for notes) and duplicate include stacks when repeated
+/// warnings occur within the same file. It also handles the logic of
+/// customizing the formatting and display of the include stack.
+///
+/// \param Level The diagnostic level of the message this stack pertains to.
+/// \param Loc   The include location of the current file (not the diagnostic
+///              location).
+void TextDiagnosticPrinter::PrintIncludeStack(Diagnostic::Level Level,
+                                              SourceLocation Loc,
+                                              const SourceManager &SM) {
+  // Skip redundant include stacks altogether.
+  if (LastWarningLoc == Loc)
+    return;
+  LastWarningLoc = Loc;
+
+  if (!DiagOpts->ShowNoteIncludeStack && Level == Diagnostic::Note)
+    return;
+
+  PrintIncludeStackRecursively(OS, SM, Loc, DiagOpts->ShowLocation);
+}
+
 /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
 /// any characters in LineNo that intersect the SourceRange.
 void TextDiagnosticPrinter::HighlightRange(const CharSourceRange &R,
@@ -398,10 +425,7 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
 
       // If this diagnostic is not in the main file, print out the
       // "included from" lines.
-      if (LastWarningLoc != PLoc.getIncludeLoc()) {
-        LastWarningLoc = PLoc.getIncludeLoc();
-        PrintIncludeStack(Diagnostic::Note, LastWarningLoc, SM);
-      }
+      PrintIncludeStack(Diagnostic::Note, PLoc.getIncludeLoc(), SM);
 
       if (DiagOpts->ShowLocation) {
         // Emit the file/line/column that this expansion came from.
@@ -879,11 +903,8 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
 
       // First, if this diagnostic is not in the main file, print out the
       // "included from" lines.
-      if (LastWarningLoc != PLoc.getIncludeLoc()) {
-        LastWarningLoc = PLoc.getIncludeLoc();
-        PrintIncludeStack(Level, LastWarningLoc, SM);
-        StartOfLocationInfo = OS.tell();
-      }
+      PrintIncludeStack(Level, PLoc.getIncludeLoc(), SM);
+      StartOfLocationInfo = OS.tell();
 
       // Compute the column number.
       if (DiagOpts->ShowLocation) {