Change Lexer::MeasureTokenLength to take a LangOptions reference.
This allows it to accurately measure tokens, so that we get:

t.cpp:8:13: error: unknown type name 'X'
static foo::X  P;
       ~~~~~^

instead of the woefully inferior:

t.cpp:8:13: error: unknown type name 'X'
static foo::X  P;
       ~~~~ ^

Most of this is just plumbing to push the reference around.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69099 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/clang-cc/HTMLPrint.cpp b/tools/clang-cc/HTMLPrint.cpp
index b2bb29c..0e15973 100644
--- a/tools/clang-cc/HTMLPrint.cpp
+++ b/tools/clang-cc/HTMLPrint.cpp
@@ -52,7 +52,7 @@
 }
 
 void HTMLPrinter::Initialize(ASTContext &context) {
-  R.setSourceMgr(context.getSourceManager());
+  R.setSourceMgr(context.getSourceManager(), context.getLangOptions());
 }
 
 HTMLPrinter::~HTMLPrinter() {
diff --git a/tools/clang-cc/RewriteBlocks.cpp b/tools/clang-cc/RewriteBlocks.cpp
index 3610da6..f1f53e4 100644
--- a/tools/clang-cc/RewriteBlocks.cpp
+++ b/tools/clang-cc/RewriteBlocks.cpp
@@ -198,7 +198,7 @@
   MainFileStart = MainBuf->getBufferStart();
   MainFileEnd = MainBuf->getBufferEnd();
   
-  Rewrite.setSourceMgr(Context->getSourceManager());
+  Rewrite.setSourceMgr(Context->getSourceManager(), LangOpts);
   
   if (IsHeader)
     Preamble = "#pragma once\n";
diff --git a/tools/clang-cc/RewriteMacros.cpp b/tools/clang-cc/RewriteMacros.cpp
index 149a73c..d601f86 100644
--- a/tools/clang-cc/RewriteMacros.cpp
+++ b/tools/clang-cc/RewriteMacros.cpp
@@ -90,7 +90,7 @@
   SourceManager &SM = PP.getSourceManager();
   
   Rewriter Rewrite;
-  Rewrite.setSourceMgr(SM);
+  Rewrite.setSourceMgr(SM, PP.getLangOptions());
   RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID());
 
   std::vector<Token> RawTokens;
diff --git a/tools/clang-cc/RewriteObjC.cpp b/tools/clang-cc/RewriteObjC.cpp
index 44c0eb3..41f532b 100644
--- a/tools/clang-cc/RewriteObjC.cpp
+++ b/tools/clang-cc/RewriteObjC.cpp
@@ -455,7 +455,7 @@
   MainFileStart = MainBuf->getBufferStart();
   MainFileEnd = MainBuf->getBufferEnd();
      
-  Rewrite.setSourceMgr(Context->getSourceManager());
+  Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions());
   
   // declaring objc_selector outside the parameter list removes a silly
   // scope related warning...
@@ -2673,7 +2673,7 @@
   // have no ivars (thus not synthesized) then no need to synthesize this class.
   if ((CDecl->isForwardDecl() || NumIvars == 0) &&
       (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) {
-    endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
+    endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
     ReplaceText(LocStart, endBuf-startBuf, Result.c_str(), Result.size());
     return;
   }
@@ -2708,7 +2708,7 @@
       SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : 
                                   CDecl->getClassLoc();
       const char *endHeader = SM->getCharacterData(L);
-      endHeader += Lexer::MeasureTokenLength(L, *SM);
+      endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts);
 
       if (CDecl->protocol_begin() != CDecl->protocol_end()) {
         // advance to the end of the referenced protocols.
@@ -2770,7 +2770,7 @@
     // Don't forget to add a ';'!!
     InsertText(LocEnd.getFileLocWithOffset(1), ";", 1);
   } else { // we don't have any instance variables - insert super struct.
-    endBuf += Lexer::MeasureTokenLength(LocEnd, *SM);
+    endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts);
     Result += " {\n    struct ";
     Result += RCDecl->getNameAsString();
     Result += "_IMPL ";
diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp
index 3360649..345a421 100644
--- a/tools/clang-cc/clang-cc.cpp
+++ b/tools/clang-cc/clang-cc.cpp
@@ -387,6 +387,33 @@
 LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
            llvm::cl::Hidden);
 
+static llvm::cl::opt<bool>
+ObjCExclusiveGC("fobjc-gc-only",
+                llvm::cl::desc("Use GC exclusively for Objective-C related "
+                               "memory management"));
+
+static llvm::cl::opt<bool>
+ObjCEnableGC("fobjc-gc",
+             llvm::cl::desc("Enable Objective-C garbage collection"));
+
+static llvm::cl::opt<LangOptions::VisibilityMode>
+SymbolVisibility("fvisibility",
+                 llvm::cl::desc("Set the default symbol visibility:"),
+                 llvm::cl::init(LangOptions::Default),
+                 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
+                                             "Use default symbol visibility"),
+                                  clEnumValN(LangOptions::Hidden, "hidden",
+                                             "Use hidden symbol visibility"),
+                                  clEnumValN(LangOptions::Protected,"protected",
+                                             "Use protected symbol visibility"),
+                                  clEnumValEnd));
+
+static llvm::cl::opt<bool>
+OverflowChecking("ftrapv",
+                 llvm::cl::desc("Trap on integer overflow"),
+                 llvm::cl::init(false));
+
+
 /// InitializeBaseLanguage - Handle the -x foo options.
 static void InitializeBaseLanguage() {
   if (LangObjC)
@@ -481,6 +508,14 @@
     Options.CPlusPlus = 1;
     break;
   }
+  
+  if (ObjCExclusiveGC)
+    Options.setGCMode(LangOptions::GCOnly);
+  else if (ObjCEnableGC)
+    Options.setGCMode(LangOptions::HybridGC);
+  
+  Options.setVisibilityMode(SymbolVisibility);
+  Options.OverflowChecking = OverflowChecking;
 }
 
 /// LangStds - Language standards we support.
@@ -796,39 +831,6 @@
     Options.setMainFileName(MainFileName.c_str());
 }
 
-static llvm::cl::opt<bool>
-ObjCExclusiveGC("fobjc-gc-only",
-                llvm::cl::desc("Use GC exclusively for Objective-C related "
-                               "memory management"));
-
-static llvm::cl::opt<bool>
-ObjCEnableGC("fobjc-gc",
-             llvm::cl::desc("Enable Objective-C garbage collection"));
-
-void InitializeGCMode(LangOptions &Options) {
-  if (ObjCExclusiveGC)
-    Options.setGCMode(LangOptions::GCOnly);
-  else if (ObjCEnableGC)
-    Options.setGCMode(LangOptions::HybridGC);
-}
-
-static llvm::cl::opt<LangOptions::VisibilityMode>
-SymbolVisibility("fvisibility",
-                 llvm::cl::desc("Set the default symbol visibility:"),
-                 llvm::cl::init(LangOptions::Default),
-                 llvm::cl::values(clEnumValN(LangOptions::Default, "default",
-                                             "Use default symbol visibility"),
-                                  clEnumValN(LangOptions::Hidden, "hidden",
-                                             "Use hidden symbol visibility"),
-                                  clEnumValN(LangOptions::Protected,"protected",
-                                             "Use protected symbol visibility"),
-                                  clEnumValEnd));
-
-static llvm::cl::opt<bool>
-OverflowChecking("ftrapv",
-           llvm::cl::desc("Trap on integer overflow"),
-           llvm::cl::init(false));
-
 //===----------------------------------------------------------------------===//
 // Target Triple Processing.
 //===----------------------------------------------------------------------===//
@@ -2021,7 +2023,8 @@
     llvm::TimeRegion Timer(ClangFrontendTimer);
     Consumer.reset(new ASTConsumer());
     FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
-                                     PP.getSourceManager());
+                                     PP.getSourceManager(),
+                                     PP.getLangOptions());
     break;
   }
 
@@ -2034,7 +2037,8 @@
       // locations now.
       if (!FixItRewrite)
         FixItRewrite = new FixItRewriter(PP.getDiagnostics(),
-                                         PP.getSourceManager());
+                                         PP.getSourceManager(),
+                                         PP.getLangOptions());
 
       bool AddedFixitLocation = false;
       for (unsigned Idx = 0, Last = FixItAtLocations.size(); 
@@ -2213,9 +2217,6 @@
   if (InputFilenames.empty())
     InputFilenames.push_back("-");
   
-  // Create a file manager object to provide access to and cache the filesystem.
-  FileManager FileMgr;
-  
   // Create the diagnostic client for reporting errors or for
   // implementing -verify.
   DiagnosticClient* TextDiagClient = 0;
@@ -2268,6 +2269,9 @@
     
   llvm::OwningPtr<SourceManager> SourceMgr;
   
+  // Create a file manager object to provide access to and cache the filesystem.
+  FileManager FileMgr;
+  
   for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
     const std::string &InFile = InputFilenames[i];
     
@@ -2286,17 +2290,21 @@
     
     // Initialize language options, inferring file types from input filenames.
     LangOptions LangInfo;
+    
+    if (!VerifyDiagnostics)
+      static_cast<TextDiagnosticPrinter*>(TextDiagClient)
+        ->SetLangOpts(LangInfo);
+
+    
     InitializeBaseLanguage();
     LangKind LK = GetLanguage(InFile);
     InitializeLangOptions(LangInfo, LK);
-    InitializeGCMode(LangInfo);
-    LangInfo.setVisibilityMode(SymbolVisibility);
-    LangInfo.OverflowChecking = OverflowChecking;
     InitializeLanguageStandard(LangInfo, LK, Target.get());
           
     // Process the -I options and set them in the HeaderInfo.
     HeaderSearch HeaderInfo(FileMgr);
     
+    
     InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo);
     
     // Set up the preprocessor with these options.