Mega-patch: ripped SourceManager out of Diagnostic/DiagnosticClient.  Now
SourceManager is passed by reference, allowing the SourceManager to be
associated with a specific translation unit, and not the entire execution
of the driver.

Modified all users of Diagnostics to comply with this new interface.

Integrated SourceManager as a member variable of TargetInfo. TargetInfo will
eventually be associated with a single translation unit (just like
SourceManager).

Made the SourceManager reference in ASTContext private. Provided accessor
getSourceManager() for clients to use instead. Modified clients to comply with
new interface.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44878 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp
index 5cd3f77..85140bc 100644
--- a/Driver/ASTConsumers.cpp
+++ b/Driver/ASTConsumers.cpp
@@ -356,7 +356,7 @@
     ASTDumper() : DeclPrinter() {}
     
     void Initialize(ASTContext &Context, unsigned MainFileID) {
-      SM = &Context.SourceMgr;
+      SM = &Context.getSourceManager();
     }
     
     virtual void HandleTopLevelDecl(Decl *D) {
@@ -400,7 +400,7 @@
     SourceManager *SM;
   public:
     void Initialize(ASTContext &Context, unsigned MainFileID) {
-      SM = &Context.SourceMgr;
+      SM = &Context.getSourceManager();
     }
     
     virtual void HandleTopLevelDecl(Decl *D) {
@@ -482,7 +482,7 @@
     SourceManager *SM;
   public:
     virtual void Initialize(ASTContext &Context, unsigned MainFileID) {
-      SM = &Context.SourceMgr;
+      SM = &Context.getSourceManager();
     }
 
     virtual void VisitCFG(CFG& C) {
diff --git a/Driver/RewriteTest.cpp b/Driver/RewriteTest.cpp
index 77adafe..cc10ab0 100644
--- a/Driver/RewriteTest.cpp
+++ b/Driver/RewriteTest.cpp
@@ -64,7 +64,7 @@
   public:
     void Initialize(ASTContext &context, unsigned mainFileID) {
       Context = &context;
-      SM = &Context->SourceMgr;
+      SM = &Context->getSourceManager();
       MsgSendFunctionDecl = 0;
       MsgSendSuperFunctionDecl = 0;
       MsgSendStretFunctionDecl = 0;
@@ -87,7 +87,7 @@
       MainFileEnd = MainBuf->getBufferEnd();
       
       
-      Rewrite.setSourceMgr(Context->SourceMgr);
+      Rewrite.setSourceMgr(Context->getSourceManager());
       // declaring objc_selector outside the parameter list removes a silly
       // scope related warning...
       const char *s = "struct objc_selector; struct objc_class;\n"
@@ -917,7 +917,8 @@
     unsigned DiagID = Diags.getCustomDiagID(Diagnostic::Error, 
                      "rewriter could not replace sub-expression due to macros");
     SourceRange Range = Exp->getSourceRange();
-    Diags.Report(Exp->getAtLoc(), DiagID, 0, 0, &Range, 1);
+    Diags.Report(Exp->getAtLoc(), DiagID, Context->getSourceManager(),
+                 0, 0, &Range, 1);
     delete Replacement;
     return Exp;
   }
diff --git a/Driver/Targets.cpp b/Driver/Targets.cpp
index 23f8307..6e810ef 100644
--- a/Driver/Targets.cpp
+++ b/Driver/Targets.cpp
@@ -702,7 +702,8 @@
 
 /// CreateTargetInfo - Return the set of target info objects as specified by
 /// the -arch command line option.
-TargetInfo *clang::CreateTargetInfo(const std::vector<std::string>& triples, 
+TargetInfo *clang::CreateTargetInfo(SourceManager& SrcMgr,
+                                    const std::vector<std::string>& triples, 
                                     Diagnostic *Diags) {
 
   assert (!triples.empty() && "No target triple.");
@@ -713,7 +714,7 @@
   if (!PrimaryTarget)
     return NULL;
   
-  TargetInfo *TI = new TargetInfo(PrimaryTarget, Diags);
+  TargetInfo *TI = new TargetInfo(SrcMgr, PrimaryTarget, Diags);
   
   // Add all secondary targets.
   for (unsigned i = 1, e = triples.size(); i != e; ++i) {
diff --git a/Driver/TextDiagnosticBuffer.cpp b/Driver/TextDiagnosticBuffer.cpp
index d0e2f90..c77801b 100644
--- a/Driver/TextDiagnosticBuffer.cpp
+++ b/Driver/TextDiagnosticBuffer.cpp
@@ -21,6 +21,7 @@
                                             Diagnostic::Level Level,
                                             SourceLocation Pos,
                                             diag::kind ID,
+                                            SourceManager& SrcMgr,
                                             const std::string *Strs,
                                             unsigned NumStrs,
                                             const SourceRange *,
diff --git a/Driver/TextDiagnosticBuffer.h b/Driver/TextDiagnosticBuffer.h
index d155874..5da37c3 100644
--- a/Driver/TextDiagnosticBuffer.h
+++ b/Driver/TextDiagnosticBuffer.h
@@ -30,7 +30,7 @@
 private:
   DiagList Errors, Warnings;
 public:
-  TextDiagnosticBuffer(SourceManager &SM) : TextDiagnostics(SM) {}
+  TextDiagnosticBuffer() {}
 
   const_iterator err_begin() const  { return Errors.begin(); }
   const_iterator err_end() const    { return Errors.end(); }
@@ -40,7 +40,9 @@
 
   virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel,
                                 SourceLocation Pos,
-                                diag::kind ID, const std::string *Strs,
+                                diag::kind ID,
+                                SourceManager& SrcMgr,
+                                const std::string *Strs,
                                 unsigned NumStrs,
                                 const SourceRange *Ranges, 
                                 unsigned NumRanges);
diff --git a/Driver/TextDiagnosticPrinter.cpp b/Driver/TextDiagnosticPrinter.cpp
index 463a3d7..d2eaaca 100644
--- a/Driver/TextDiagnosticPrinter.cpp
+++ b/Driver/TextDiagnosticPrinter.cpp
@@ -31,13 +31,13 @@
                                   " diagnostics"));
 
 void TextDiagnosticPrinter::
-PrintIncludeStack(SourceLocation Pos) {
+PrintIncludeStack(SourceLocation Pos, SourceManager& SourceMgr) {
   if (Pos.isInvalid()) return;
 
   Pos = SourceMgr.getLogicalLoc(Pos);
 
   // Print out the other include frames first.
-  PrintIncludeStack(SourceMgr.getIncludeLoc(Pos));
+  PrintIncludeStack(SourceMgr.getIncludeLoc(Pos),SourceMgr);
   unsigned LineNo = SourceMgr.getLineNumber(Pos);
   
   std::cerr << "In file included from " << SourceMgr.getSourceName(Pos)
@@ -46,7 +46,8 @@
 
 /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
 /// any characters in LineNo that intersect the SourceRange.
-void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, 
+void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
+                                           SourceManager& SourceMgr,
                                            unsigned LineNo,
                                            std::string &CaratLine,
                                            const std::string &SourceLine) {
@@ -101,6 +102,7 @@
                                              Diagnostic::Level Level, 
                                              SourceLocation Pos,
                                              diag::kind ID,
+                                             SourceManager& SourceMgr,
                                              const std::string *Strs,
                                              unsigned NumStrs,
                                              const SourceRange *Ranges,
@@ -116,7 +118,7 @@
     // "included from" lines.
     if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) {
       LastWarningLoc = SourceMgr.getIncludeLoc(LPos);
-      PrintIncludeStack(LastWarningLoc);
+      PrintIncludeStack(LastWarningLoc,SourceMgr);
     }
   
     // Compute the column number.  Rewind from the current position to the start
@@ -162,7 +164,7 @@
     
     // Highlight all of the characters covered by Ranges with ~ characters.
     for (unsigned i = 0; i != NumRanges; ++i)
-      HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
+      HighlightRange(Ranges[i], SourceMgr, LineNo, CaratLine, SourceLine);
     
     // Next, insert the carat itself.
     if (ColNo-1 < CaratLine.size())
diff --git a/Driver/TextDiagnosticPrinter.h b/Driver/TextDiagnosticPrinter.h
index beb9d80..c87c20e 100644
--- a/Driver/TextDiagnosticPrinter.h
+++ b/Driver/TextDiagnosticPrinter.h
@@ -24,17 +24,20 @@
 class TextDiagnosticPrinter : public TextDiagnostics {
   SourceLocation LastWarningLoc;
 public:
-  TextDiagnosticPrinter(SourceManager &sourceMgr)
-    : TextDiagnostics(sourceMgr) {}
+  TextDiagnosticPrinter() {}
 
-  void PrintIncludeStack(SourceLocation Pos);
-  void HighlightRange(const SourceRange &R, unsigned LineNo,
+  void PrintIncludeStack(SourceLocation Pos, SourceManager& SrcMgr);
+  void HighlightRange(const SourceRange &R,
+                      SourceManager& SrcMgr,
+                      unsigned LineNo,
                       std::string &CaratLine,
                       const std::string &SourceLine);
 
   virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel,
                                 SourceLocation Pos,
-                                diag::kind ID, const std::string *Strs,
+                                diag::kind ID,
+                                SourceManager& SrcMgr,
+                                const std::string *Strs,
                                 unsigned NumStrs,
                                 const SourceRange *Ranges, 
                                 unsigned NumRanges);
diff --git a/Driver/TextDiagnostics.cpp b/Driver/TextDiagnostics.cpp
index 5b14cdd..46e535d 100644
--- a/Driver/TextDiagnostics.cpp
+++ b/Driver/TextDiagnostics.cpp
@@ -40,7 +40,8 @@
 }
 
 bool TextDiagnostics::IgnoreDiagnostic(Diagnostic::Level Level,
-                                       SourceLocation Pos) {
+                                       SourceLocation Pos,
+                                       SourceManager& SourceMgr) {
   if (Pos.isValid()) {
     // If this is a warning or note, and if it a system header, suppress the
     // diagnostic.
diff --git a/Driver/TextDiagnostics.h b/Driver/TextDiagnostics.h
index 27d9d3a..ad5e4cb 100644
--- a/Driver/TextDiagnostics.h
+++ b/Driver/TextDiagnostics.h
@@ -24,23 +24,25 @@
 class TextDiagnostics : public DiagnosticClient {
   HeaderSearch *TheHeaderSearch;
 protected:
-  SourceManager &SourceMgr;
-
   std::string FormatDiagnostic(Diagnostic &Diags, Diagnostic::Level Level,
                                diag::kind ID,
                                const std::string *Strs,
                                unsigned NumStrs);
 public:
-  TextDiagnostics(SourceManager &sourceMgr) : SourceMgr(sourceMgr) {}
+  TextDiagnostics() {}
   virtual ~TextDiagnostics();
 
   void setHeaderSearch(HeaderSearch &HS) { TheHeaderSearch = &HS; }
 
   virtual bool IgnoreDiagnostic(Diagnostic::Level Level, 
-                                SourceLocation Pos);
+                                SourceLocation Pos,
+                                SourceManager& SrcMgr);
+
   virtual void HandleDiagnostic(Diagnostic &Diags, Diagnostic::Level DiagLevel,
                                 SourceLocation Pos,
-                                diag::kind ID, const std::string *Strs,
+                                diag::kind ID,
+                                SourceManager& SrcMgr,
+                                const std::string *Strs,
                                 unsigned NumStrs,
                                 const SourceRange *Ranges, 
                                 unsigned NumRanges) = 0;
diff --git a/Driver/TranslationUnit.cpp b/Driver/TranslationUnit.cpp
index 627657a..6f35a32 100644
--- a/Driver/TranslationUnit.cpp
+++ b/Driver/TranslationUnit.cpp
@@ -102,7 +102,7 @@
   Sezr.EnterBlock();
   
   // Emit the SourceManager.
-  Sezr.Emit(Context->SourceMgr);
+  Sezr.Emit(Context->getSourceManager());
   
   // Emit the LangOptions.
   Sezr.Emit(LangOpts);
@@ -184,7 +184,7 @@
   assert (FoundBlock);
 
   // Read the SourceManager.
-  SourceManager::CreateAndRegister(Dezr,FMgr);
+  SourceManager& SrcMgr = *SourceManager::CreateAndRegister(Dezr,FMgr);
   
   // Read the LangOptions.
   TU->LangOpts.Read(Dezr);
@@ -196,7 +196,7 @@
     std::vector<std::string> triples;
     triples.push_back(triple);
     delete [] triple;
-    Dezr.RegisterPtr(PtrID,CreateTargetInfo(triples,NULL));
+    Dezr.RegisterPtr(PtrID,CreateTargetInfo(SrcMgr,triples,NULL));
   }
   
   // For Selectors, we must read the identifier table first because the
diff --git a/Driver/clang.cpp b/Driver/clang.cpp
index eb469d8..c009e06 100644
--- a/Driver/clang.cpp
+++ b/Driver/clang.cpp
@@ -989,10 +989,10 @@
   std::auto_ptr<TextDiagnostics> DiagClient;
   if (!VerifyDiagnostics) {
     // Print diagnostics to stderr by default.
-    DiagClient.reset(new TextDiagnosticPrinter(SourceMgr));
+    DiagClient.reset(new TextDiagnosticPrinter());
   } else {
     // When checking diagnostics, just buffer them up.
-    DiagClient.reset(new TextDiagnosticBuffer(SourceMgr));
+    DiagClient.reset(new TextDiagnosticBuffer());
    
     if (InputFilenames.size() != 1) {
       fprintf(stderr,
@@ -1013,7 +1013,7 @@
   { // Create triples, and create the TargetInfo.
     std::vector<std::string> triples;
     CreateTargetTriples(triples);
-    Target = CreateTargetInfo(triples,&Diags);
+    Target = CreateTargetInfo(SourceMgr,triples,&Diags);
   
     if (Target == 0) {
       fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
@@ -1026,7 +1026,9 @@
   // -I- is a deprecated GCC feature, scan for it and reject it.
   for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
     if (I_dirs[i] == "-") {
-      Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported);
+      Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported,
+                   SourceMgr);
+      
       I_dirs.erase(I_dirs.begin()+i);
       --i;
     }
diff --git a/Driver/clang.h b/Driver/clang.h
index 098c51b..c4bbb84 100644
--- a/Driver/clang.h
+++ b/Driver/clang.h
@@ -25,6 +25,7 @@
 class Diagnostic;
 class ASTConsumer;
 class IdentifierTable;
+class SourceManager;
 
 /// DoPrintPreprocessedInput - Implement -E mode.
 void DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
@@ -36,7 +37,8 @@
 
 /// CreateTargetInfo - Return the set of target info objects as specified by
 /// the -arch command line option.
-TargetInfo *CreateTargetInfo(const std::vector<std::string>& triples,
+TargetInfo *CreateTargetInfo(SourceManager& SrcMgr,
+                             const std::vector<std::string>& triples,
                              Diagnostic *Diags);
 
 /// EmitLLVMFromASTs - Implement -emit-llvm, which generates llvm IR from C.