* Remove isInSystemHeader() from DiagClient, move it to SourceManager
* Move FormatError() from TextDiagnostic up to DiagClient, remove now  
  empty class TextDiagnostic
* Make DiagClient optional for Diagnostic

This fixes the following problems:

* -html-diags (and probably others) does now output the same set of  
  warnings as console clang does
* nothing crashes if one forgets to call setHeaderSearch() on  
  TextDiagnostic
* some code duplication is removed




git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@54620 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/PathDiagnostic.cpp b/lib/Analysis/PathDiagnostic.cpp
index 28b76b3..4c34953 100644
--- a/lib/Analysis/PathDiagnostic.cpp
+++ b/lib/Analysis/PathDiagnostic.cpp
@@ -33,9 +33,6 @@
   
   PathDiagnostic* D = new PathDiagnostic();
   
-  // Ripped from TextDiagnostics::FormatDiagnostic.  Perhaps we should
-  // centralize it somewhere?
-  
   std::ostringstream os;
   
   switch (DiagLevel) {
@@ -46,17 +43,8 @@
     case Diagnostic::Fatal:   os << "fatal error: "; break;
       break;
   }
-  
-  std::string Msg = Diags.getDescription(ID);
 
-  for (unsigned i = 0; i < Msg.size() - 1; ++i) {
-    if (Msg[i] == '%' && isdigit(Msg[i + 1])) {
-      unsigned StrNo = Msg[i + 1] - '0';
-      Msg = std::string(Msg.begin(), Msg.begin() + i) +
-      (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") +
-      std::string(Msg.begin() + i + 2, Msg.end());
-    }
-  }
+  std::string Msg = FormatDiagnostic(Diags, DiagLevel, ID, Strs, NumStrs);
   
   os << Msg;
   
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index 16bdd4a..f9e1d2b 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -203,11 +203,10 @@
   }
 }
 
-/// Report - Issue the message to the client. If the client wants us to stop
-/// compilation, return true, otherwise return false.  DiagID is a member of
-/// the diag::kind enum.  
+/// Report - Issue the message to the client.
+///  DiagID is a member of the diag::kind enum.  
 void Diagnostic::Report(DiagnosticClient* C,
-                        FullSourceLoc Pos, unsigned DiagID,
+                        FullSourceLoc Loc, unsigned DiagID,
                         const std::string *Strs, unsigned NumStrs,
                         const SourceRange *Ranges, unsigned NumRanges) {
   
@@ -217,33 +216,55 @@
   // If the client doesn't care about this message, don't issue it.
   if (DiagLevel == Diagnostic::Ignored)
     return;
-  
+
   // Set the diagnostic client if it isn't set already.
   if (!C) C = Client;
 
-  // If this is not an error and we are in a system header, ignore it.  We have
-  // to check on the original class here, because we also want to ignore
-  // extensions and warnings in -Werror and -pedantic-errors modes, which *map*
-  // warnings/extensions to errors.
+  // If this is not an error and we are in a system header, ignore it.  We
+  // have to check on the original DiagID here, because we also want to
+  // ignore extensions and warnings in -Werror and -pedantic-errors modes,
+  // which *map* warnings/extensions to errors.
   if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS &&
       getBuiltinDiagClass(DiagID) != ERROR &&
-      Client->isInSystemHeader(Pos))
+      Loc.isValid() && Loc.isFileID() && Loc.isInSystemHeader())
     return;
   
   if (DiagLevel >= Diagnostic::Error) {
     ErrorOccurred = true;
-    
-    if (C == Client)
+
+    if (C != 0 && C == Client)
       ++NumErrors;
   }
 
   // Finally, report it.
-  
-  C->HandleDiagnostic(*this, DiagLevel, Pos, (diag::kind)DiagID,
-                      Strs, NumStrs, Ranges, NumRanges);
-  
-  if (C == Client)
+ 
+  if (C != 0)
+    C->HandleDiagnostic(*this, DiagLevel, Loc, (diag::kind)DiagID,
+                        Strs, NumStrs, Ranges, NumRanges);
+
+  if (C != 0 && C == Client)
     ++NumDiagnostics;
 }
 
+
 DiagnosticClient::~DiagnosticClient() {}
+
+std::string DiagnosticClient::FormatDiagnostic(Diagnostic &Diags,
+                                               Diagnostic::Level Level,
+                                               diag::kind ID,
+                                               const std::string *Strs,
+                                               unsigned NumStrs) {
+  std::string Msg = Diags.getDescription(ID);
+  
+  // Replace all instances of %0 in Msg with 'Extra'.
+  for (unsigned i = 0; i < Msg.size() - 1; ++i) {
+    if (Msg[i] == '%' && isdigit(Msg[i + 1])) {
+      unsigned StrNo = Msg[i + 1] - '0';
+      Msg = std::string(Msg.begin(), Msg.begin() + i) +
+            (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") +
+            std::string(Msg.begin() + i + 2, Msg.end());
+    }
+  }
+
+  return Msg;
+}
diff --git a/lib/Basic/SourceLocation.cpp b/lib/Basic/SourceLocation.cpp
index 83c264a..12a4962 100644
--- a/lib/Basic/SourceLocation.cpp
+++ b/lib/Basic/SourceLocation.cpp
@@ -79,6 +79,12 @@
   return SrcMgr->getFileEntryForLoc(Loc);
 }
 
+bool FullSourceLoc::isInSystemHeader() const {
+  assert (isValid());
+  return SrcMgr->isInSystemHeader(Loc);
+}
+
+
 const char * FullSourceLoc::getCharacterData() const {
   assert (isValid());
   return SrcMgr->getCharacterData(Loc);
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp
index d7d2c84..7534ac4 100644
--- a/lib/Basic/SourceManager.cpp
+++ b/lib/Basic/SourceManager.cpp
@@ -75,14 +75,15 @@
 /// include position.  This works regardless of whether the ContentCache
 /// corresponds to a file or some other input source.
 unsigned SourceManager::createFileID(const ContentCache *File,
-                                     SourceLocation IncludePos) {
+                                     SourceLocation IncludePos,
+                                     bool isSysHeader) {
   // If FileEnt is really large (e.g. it's a large .i file), we may not be able
   // to fit an arbitrary position in the file in the FilePos field.  To handle
   // this, we create one FileID for each chunk of the file that fits in a
   // FilePos field.
   unsigned FileSize = File->Buffer->getBufferSize();
   if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
-    FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File));
+    FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File, isSysHeader));
     assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
            "Ran out of file ID's!");
     return FileIDs.size();
@@ -93,7 +94,8 @@
 
   unsigned ChunkNo = 0;
   while (1) {
-    FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File));
+    FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File,
+                                      isSysHeader));
 
     if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
     FileSize -= (1 << SourceLocation::FilePosBits);
diff --git a/lib/Driver/TextDiagnosticPrinter.cpp b/lib/Driver/TextDiagnosticPrinter.cpp
index f8f6b35..5b99f70 100644
--- a/lib/Driver/TextDiagnosticPrinter.cpp
+++ b/lib/Driver/TextDiagnosticPrinter.cpp
@@ -14,7 +14,6 @@
 #include "clang/Driver/TextDiagnosticPrinter.h"
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/SourceManager.h"
-#include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/Lexer.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include <string>
diff --git a/lib/Driver/TextDiagnostics.cpp b/lib/Driver/TextDiagnostics.cpp
deleted file mode 100644
index ae7b57d..0000000
--- a/lib/Driver/TextDiagnostics.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-//===--- TextDiagnostics.cpp - Text Diagnostics Parent Class --------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This is the parent class for all text diagnostics.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Driver/TextDiagnostics.h"
-#include "clang/Basic/SourceLocation.h"
-#include "clang/Basic/SourceManager.h"
-#include "clang/Lex/HeaderSearch.h"
-using namespace clang;
-
-TextDiagnostics:: ~TextDiagnostics() {}
-
-std::string TextDiagnostics::FormatDiagnostic(Diagnostic &Diags,
-                                              Diagnostic::Level Level,
-                                              diag::kind ID,
-                                              const std::string *Strs,
-                                              unsigned NumStrs) {
-  std::string Msg = Diags.getDescription(ID);
-  
-  // Replace all instances of %0 in Msg with 'Extra'.
-  for (unsigned i = 0; i < Msg.size() - 1; ++i) {
-    if (Msg[i] == '%' && isdigit(Msg[i + 1])) {
-      unsigned StrNo = Msg[i + 1] - '0';
-      Msg = std::string(Msg.begin(), Msg.begin() + i) +
-            (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") +
-            std::string(Msg.begin() + i + 2, Msg.end());
-    }
-  }
-
-  return Msg;
-}
-
-bool TextDiagnostics::isInSystemHeader(FullSourceLoc Pos) const {
-  if (!Pos.isValid()) return false;
-  
-  if (const FileEntry *F = Pos.getFileEntryForLoc()) {
-    DirectoryLookup::DirType DirInfo = TheHeaderSearch->getFileDirFlavor(F);
-    if (DirInfo == DirectoryLookup::SystemHeaderDir ||
-        DirInfo == DirectoryLookup::ExternCSystemHeaderDir)
-      return true;
-  }
-
-  return false;
-}
diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp
index 11afa84..2aa8eae 100644
--- a/lib/Lex/PPDirectives.cpp
+++ b/lib/Lex/PPDirectives.cpp
@@ -674,7 +674,8 @@
   }
 
   // Look up the file, create a File ID for it.
-  unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation());
+  unsigned FileID = SourceMgr.createFileID(File, FilenameTok.getLocation(),
+                                           isSystemHeader(File));
   if (FileID == 0)
     return Diag(FilenameTok, diag::err_pp_file_not_found,
                 std::string(FilenameStart, FilenameEnd));
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 33c94b6..bc28442 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -115,6 +115,17 @@
   delete Callbacks;
 }
 
+bool Preprocessor::isSystemHeader(const FileEntry* F) const {
+  if (F) {
+    DirectoryLookup::DirType DirInfo = HeaderInfo.getFileDirFlavor(F);
+    if (DirInfo == DirectoryLookup::SystemHeaderDir ||
+        DirInfo == DirectoryLookup::ExternCSystemHeaderDir)
+      return true;
+  }
+  return false;
+}
+
+
 /// Diag - Forwarding function for diagnostics.  This emits a diagnostic at
 /// the specified Token's location, translating the token's start
 /// position in the current buffer into a SourcePosition object for rendering.