From: Chris Lattner Date: Tue, 14 Apr 2009 23:22:57 +0000 (+0000) Subject: Change Lexer::MeasureTokenLength to take a LangOptions reference. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c78b873f4f3823ae859c15674cb3d76c8554113;p=clang 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/include/clang/Frontend/FixItRewriter.h b/include/clang/Frontend/FixItRewriter.h index 363b9ad9a3..7fcd682bf6 100644 --- a/include/clang/Frontend/FixItRewriter.h +++ b/include/clang/Frontend/FixItRewriter.h @@ -57,7 +57,8 @@ class FixItRewriter : public DiagnosticClient { public: /// \brief Initialize a new fix-it rewriter. - FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr); + FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr, + const LangOptions &LangOpts); /// \brief Destroy the fix-it rewriter. ~FixItRewriter(); diff --git a/include/clang/Frontend/TextDiagnosticPrinter.h b/include/clang/Frontend/TextDiagnosticPrinter.h index 0f08a95283..f0cc4a0ca0 100644 --- a/include/clang/Frontend/TextDiagnosticPrinter.h +++ b/include/clang/Frontend/TextDiagnosticPrinter.h @@ -24,28 +24,37 @@ namespace llvm { namespace clang { class SourceManager; +class LangOptions; class TextDiagnosticPrinter : public DiagnosticClient { + llvm::raw_ostream &OS; + const LangOptions *LangOpts; SourceLocation LastWarningLoc; FullSourceLoc LastLoc; bool LastCaretDiagnosticWasNote; - llvm::raw_ostream &OS; + bool ShowColumn; bool CaretDiagnostics; bool ShowLocation; bool PrintRangeInfo; public: - TextDiagnosticPrinter(llvm::raw_ostream &os, bool showColumn = true, + TextDiagnosticPrinter(llvm::raw_ostream &os, + bool showColumn = true, bool caretDiagnistics = true, bool showLocation = true, bool printRangeInfo = true) - : LastCaretDiagnosticWasNote(false), OS(os), ShowColumn(showColumn), + : OS(os), LangOpts(0), + LastCaretDiagnosticWasNote(false), ShowColumn(showColumn), CaretDiagnostics(caretDiagnistics), ShowLocation(showLocation), PrintRangeInfo(printRangeInfo) {} + void SetLangOpts(const LangOptions &LO) { + LangOpts = &LO; + } + void PrintIncludeStack(SourceLocation Loc, const SourceManager &SM); void HighlightRange(const SourceRange &R, - const SourceManager& SrcMgr, + const SourceManager &SrcMgr, unsigned LineNo, FileID FID, std::string &CaretLine, const std::string &SourceLine); diff --git a/include/clang/Lex/Lexer.h b/include/clang/Lex/Lexer.h index 8077934a61..61a532ac53 100644 --- a/include/clang/Lex/Lexer.h +++ b/include/clang/Lex/Lexer.h @@ -211,7 +211,8 @@ public: /// includes a trigraph or an escaped newline) then this count includes bytes /// that are part of that. static unsigned MeasureTokenLength(SourceLocation Loc, - const SourceManager &SM); + const SourceManager &SM, + const LangOptions &LangOpts); //===--------------------------------------------------------------------===// // Internal implementation interfaces. diff --git a/include/clang/Rewrite/Rewriter.h b/include/clang/Rewrite/Rewriter.h index b05770b2aa..afa018d9cb 100644 --- a/include/clang/Rewrite/Rewriter.h +++ b/include/clang/Rewrite/Rewriter.h @@ -25,6 +25,7 @@ namespace clang { class SourceManager; + class LangOptions; class Rewriter; class Stmt; @@ -117,14 +118,19 @@ private: // Methods only usable by Rewriter. /// are involved. class Rewriter { SourceManager *SourceMgr; - + const LangOptions *LangOpts; std::map RewriteBuffers; public: - explicit Rewriter(SourceManager &SM) : SourceMgr(&SM) {} - explicit Rewriter() : SourceMgr(0) {} + explicit Rewriter(SourceManager &SM, const LangOptions &LO) + : SourceMgr(&SM), LangOpts(&LO) {} + explicit Rewriter() : SourceMgr(0), LangOpts(0) {} - void setSourceMgr(SourceManager &SM) { SourceMgr = &SM; } - SourceManager& getSourceMgr() { return *SourceMgr; } + void setSourceMgr(SourceManager &SM, const LangOptions &LO) { + SourceMgr = &SM; + LangOpts = &LO; + } + SourceManager &getSourceMgr() { return *SourceMgr; } + const LangOptions &getLangOpts() { return *LangOpts; } /// isRewritable - Return true if this location is a raw file location, which /// is rewritable. Locations from macros, etc are not rewritable. diff --git a/lib/Frontend/FixItRewriter.cpp b/lib/Frontend/FixItRewriter.cpp index 6fde5dae75..1ed89d75a9 100644 --- a/lib/Frontend/FixItRewriter.cpp +++ b/lib/Frontend/FixItRewriter.cpp @@ -22,8 +22,9 @@ #include "llvm/System/Path.h" using namespace clang; -FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr) - : Diags(Diags), Rewrite(SourceMgr), NumFailures(0) { +FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr, + const LangOptions &LangOpts) + : Diags(Diags), Rewrite(SourceMgr, LangOpts), NumFailures(0) { Client = Diags.getClient(); Diags.setClient(this); } diff --git a/lib/Frontend/HTMLDiagnostics.cpp b/lib/Frontend/HTMLDiagnostics.cpp index 83c2ac56a7..752d41fb4c 100644 --- a/lib/Frontend/HTMLDiagnostics.cpp +++ b/lib/Frontend/HTMLDiagnostics.cpp @@ -163,7 +163,7 @@ void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) { return; // FIXME: Emit a warning? // Create a new rewriter to generate HTML. - Rewriter R(const_cast(SMgr)); + Rewriter R(const_cast(SMgr), PP->getLangOptions()); // Process the path. unsigned n = D.size(); @@ -574,8 +574,8 @@ void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, const char *HighlightStart, const char *HighlightEnd) { - - SourceManager& SM = R.getSourceMgr(); + SourceManager &SM = R.getSourceMgr(); + const LangOptions &LangOpts = R.getLangOpts(); SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin()); unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart); @@ -596,7 +596,7 @@ void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, if (EndColNo) { // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; + EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; } // Highlight the range. Make the span tag the outermost tag for the diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index bc754cb845..b0b35e491b 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -482,14 +482,15 @@ PCHReader::PCHReadResult PCHReader::ReadSourceManagerBlock() { SourceLocation::getFromRawEncoding(Record[2]), SourceLocation::getFromRawEncoding(Record[3]), Lexer::MeasureTokenLength(SpellingLoc, - SourceMgr)); + SourceMgr, + PP.getLangOptions())); break; } - case pch::SM_LINE_TABLE: { + case pch::SM_LINE_TABLE: if (ParseLineTable(SourceMgr, Record)) return Failure; - } + break; } } } diff --git a/lib/Frontend/PlistDiagnostics.cpp b/lib/Frontend/PlistDiagnostics.cpp index d87a8f189f..726248431f 100644 --- a/lib/Frontend/PlistDiagnostics.cpp +++ b/lib/Frontend/PlistDiagnostics.cpp @@ -15,7 +15,7 @@ #include "clang/Analysis/PathDiagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" -#include "clang/Lex/Lexer.h" +#include "clang/Lex/Preprocessor.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/Casting.h" @@ -36,8 +36,9 @@ namespace { class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { std::vector BatchedDiags; const std::string OutputFile; + const LangOptions &LangOpts; public: - PlistDiagnostics(const std::string& prefix); + PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts); ~PlistDiagnostics(); void HandlePathDiagnostic(const PathDiagnostic* D); @@ -47,13 +48,14 @@ namespace { }; } // end anonymous namespace -PlistDiagnostics::PlistDiagnostics(const std::string& output) - : OutputFile(output) {} +PlistDiagnostics::PlistDiagnostics(const std::string& output, + const LangOptions &LO) + : OutputFile(output), LangOpts(LO) {} PathDiagnosticClient* clang::CreatePlistDiagnosticClient(const std::string& s, - Preprocessor*, PreprocessorFactory*) { - return new PlistDiagnostics(s); + Preprocessor *PP, PreprocessorFactory*) { + return new PlistDiagnostics(s, PP->getLangOptions()); } static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl &V, @@ -66,9 +68,9 @@ static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl &V, V.push_back(FID); } -static unsigned GetFID(const FIDMap& FIDs, const SourceManager* SM, +static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, SourceLocation L) { - FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); + FileID FID = SM.getFileID(SM.getInstantiationLoc(L)); FIDMap::const_iterator I = FIDs.find(FID); assert(I != FIDs.end()); return I->second; @@ -79,14 +81,16 @@ static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { return o; } -static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM, - SourceLocation L, const FIDMap& FM, - const unsigned indent, bool extend = false) { +static void EmitLocation(llvm::raw_ostream& o, const SourceManager &SM, + const LangOptions &LangOpts, + SourceLocation L, const FIDMap &FM, + unsigned indent, bool extend = false) { - FullSourceLoc Loc(SM->getInstantiationLoc(L), const_cast(*SM)); + FullSourceLoc Loc(SM.getInstantiationLoc(L), const_cast(SM)); // Add in the length of the token, so that we cover multi-char tokens. - unsigned offset = extend ? Lexer::MeasureTokenLength(Loc, *SM) - 1 : 0; + unsigned offset = + extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; Indent(o, indent) << "\n"; Indent(o, indent) << " line" @@ -98,19 +102,20 @@ static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM, Indent(o, indent) << "\n"; } -static void EmitLocation(llvm::raw_ostream& o, const SourceManager* SM, +static void EmitLocation(llvm::raw_ostream& o, const SourceManager &SM, + const LangOptions &LangOpts, const PathDiagnosticLocation &L, const FIDMap& FM, - const unsigned indent, bool extend = false) { - EmitLocation(o, SM, L.asLocation(), FM, indent, extend); + unsigned indent, bool extend = false) { + EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend); } -static void EmitRange(llvm::raw_ostream& o, const SourceManager* SM, - SourceRange R, const FIDMap& FM, - const unsigned indent) { - +static void EmitRange(llvm::raw_ostream& o, const SourceManager &SM, + const LangOptions &LangOpts, + SourceRange R, const FIDMap &FM, + unsigned indent) { Indent(o, indent) << "\n"; - EmitLocation(o, SM, R.getBegin(), FM, indent+1); - EmitLocation(o, SM, R.getEnd(), FM, indent+1, true); + EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); + EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, true); Indent(o, indent) << "\n"; } @@ -134,7 +139,9 @@ static llvm::raw_ostream& EmitString(llvm::raw_ostream& o, static void ReportControlFlow(llvm::raw_ostream& o, const PathDiagnosticControlFlowPiece& P, - const FIDMap& FM, const SourceManager *SM, + const FIDMap& FM, + const SourceManager &SM, + const LangOptions &LangOpts, unsigned indent) { Indent(o, indent) << "\n"; @@ -145,9 +152,9 @@ static void ReportControlFlow(llvm::raw_ostream& o, // FIXME: Eventually remove (DEPRECATED) // Output the start and end locations. Indent(o, indent) << "start\n"; - EmitLocation(o, SM, P.getStartLocation(), FM, indent); + EmitLocation(o, SM, LangOpts, P.getStartLocation(), FM, indent); Indent(o, indent) << "end\n"; - EmitLocation(o, SM, P.getEndLocation(), FM, indent); + EmitLocation(o, SM, LangOpts, P.getEndLocation(), FM, indent); // Emit edges. Indent(o, indent) << "edges\n"; @@ -159,9 +166,9 @@ static void ReportControlFlow(llvm::raw_ostream& o, Indent(o, indent) << "\n"; ++indent; Indent(o, indent) << "start\n"; - EmitRange(o, SM, I->getStart().asRange(), FM, indent+1); + EmitRange(o, SM, LangOpts, I->getStart().asRange(), FM, indent+1); Indent(o, indent) << "end\n"; - EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1); + EmitRange(o, SM, LangOpts, I->getEnd().asRange(), FM, indent+1); --indent; Indent(o, indent) << "\n"; } @@ -181,7 +188,9 @@ static void ReportControlFlow(llvm::raw_ostream& o, } static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, - const FIDMap& FM, const SourceManager* SM, + const FIDMap& FM, + const SourceManager &SM, + const LangOptions &LangOpts, unsigned indent) { Indent(o, indent) << "\n"; @@ -193,7 +202,7 @@ static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, FullSourceLoc L = P.getLocation().asLocation(); Indent(o, indent) << "location\n"; - EmitLocation(o, SM, L, FM, indent); + EmitLocation(o, SM, LangOpts, L, FM, indent); // Output the ranges (if any). PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), @@ -203,7 +212,8 @@ static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, Indent(o, indent) << "ranges\n"; Indent(o, indent) << "\n"; ++indent; - for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); + for (; RI != RE; ++RI) + EmitRange(o, SM, LangOpts, *RI, FM, indent+1); --indent; Indent(o, indent) << "\n"; } @@ -226,7 +236,8 @@ static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, static void ReportMacro(llvm::raw_ostream& o, const PathDiagnosticMacroPiece& P, - const FIDMap& FM, const SourceManager *SM, + const FIDMap& FM, const SourceManager &SM, + const LangOptions &LangOpts, unsigned indent) { for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); @@ -236,30 +247,35 @@ static void ReportMacro(llvm::raw_ostream& o, default: break; case PathDiagnosticPiece::Event: - ReportEvent(o, cast(**I), FM, SM, indent); + ReportEvent(o, cast(**I), FM, SM, LangOpts, + indent); break; case PathDiagnosticPiece::Macro: - ReportMacro(o, cast(**I), FM, SM, indent); + ReportMacro(o, cast(**I), FM, SM, LangOpts, + indent); break; } } } static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, - const FIDMap& FM, const SourceManager* SM) { + const FIDMap& FM, const SourceManager &SM, + const LangOptions &LangOpts) { unsigned indent = 4; switch (P.getKind()) { case PathDiagnosticPiece::ControlFlow: ReportControlFlow(o, cast(P), FM, SM, - indent); + LangOpts, indent); break; case PathDiagnosticPiece::Event: - ReportEvent(o, cast(P), FM, SM, indent); + ReportEvent(o, cast(P), FM, SM, LangOpts, + indent); break; case PathDiagnosticPiece::Macro: - ReportMacro(o, cast(P), FM, SM, indent); + ReportMacro(o, cast(P), FM, SM, LangOpts, + indent); break; } } @@ -352,7 +368,7 @@ PlistDiagnostics::~PlistDiagnostics() { o << " \n"; for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) - ReportDiag(o, *I, FM, SM); + ReportDiag(o, *I, FM, *SM, LangOpts); o << " \n"; @@ -366,7 +382,7 @@ PlistDiagnostics::~PlistDiagnostics() { // Output the location of the bug. o << " location\n"; - EmitLocation(o, SM, D->getLocation(), FM, 2); + EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); // Close up the entry. o << " \n"; diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp index f1555dbff3..c472a140cb 100644 --- a/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/lib/Frontend/TextDiagnosticPrinter.cpp @@ -82,7 +82,7 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, --EndColNo; // Zero base the col #. // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += Lexer::MeasureTokenLength(End, SM); + EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); } else { EndColNo = CaretLine.size(); } @@ -281,7 +281,7 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, continue; // Add in the length of the token, so that we cover multi-char tokens. - unsigned TokSize = Lexer::MeasureTokenLength(E, SM); + unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index 059afe2182..d04b1c2b69 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -216,7 +216,8 @@ void Lexer::Stringify(llvm::SmallVectorImpl &Str) { /// includes a trigraph or an escaped newline) then this count includes bytes /// that are part of that. unsigned Lexer::MeasureTokenLength(SourceLocation Loc, - const SourceManager &SM) { + const SourceManager &SM, + const LangOptions &LangOpts) { // TODO: this could be special cased for common tokens like identifiers, ')', // etc to make this faster, if it mattered. Just look at StrData[0] to handle // all obviously single-char tokens. This could use @@ -230,11 +231,6 @@ unsigned Lexer::MeasureTokenLength(SourceLocation Loc, std::pair Buffer = SM.getBufferData(LocInfo.first); const char *StrData = Buffer.first+LocInfo.second; - // Create a langops struct and enable trigraphs. This is sufficient for - // measuring tokens. - LangOptions LangOpts; - LangOpts.Trigraphs = true; - // Create a lexer starting at the beginning of this token. Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second); Token TheTok; diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 2c9b0b937a..111409ff78 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -336,7 +336,7 @@ SourceLocation Preprocessor::getLocForEndOfToken(SourceLocation Loc) { if (Loc.isInvalid() || !Loc.isFileID()) return SourceLocation(); - unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager()); + unsigned Len = Lexer::MeasureTokenLength(Loc, getSourceManager(), Features); return AdvanceToTokenCharacter(Loc, Len); } diff --git a/lib/Rewrite/HTMLRewrite.cpp b/lib/Rewrite/HTMLRewrite.cpp index 9bb5d7cfd3..69dd03ad3f 100644 --- a/lib/Rewrite/HTMLRewrite.cpp +++ b/lib/Rewrite/HTMLRewrite.cpp @@ -41,7 +41,7 @@ void html::HighlightRange(Rewriter &R, SourceLocation B, SourceLocation E, unsigned EOffset = SM.getFileOffset(E); // Include the whole end token in the range. - EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr()); + EOffset += Lexer::MeasureTokenLength(E, R.getSourceMgr(), R.getLangOpts()); HighlightRange(R.getEditBuffer(FID), BOffset, EOffset, SM.getBufferData(FID).first, StartTag, EndTag); diff --git a/lib/Rewrite/Rewriter.cpp b/lib/Rewrite/Rewriter.cpp index 551fa1d491..805c61baed 100644 --- a/lib/Rewrite/Rewriter.cpp +++ b/lib/Rewrite/Rewriter.cpp @@ -93,7 +93,7 @@ int Rewriter::getRangeSize(SourceRange Range) const { // Adjust the end offset to the end of the last token, instead of being the // start of the last token. - EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr); + EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); return EndOff-StartOff; } @@ -127,7 +127,7 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { // Adjust the end offset to the end of the last token, instead of being the // start of the last token. - EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr); + EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); return std::string(Ptr, Ptr+EndOff-StartOff); } @@ -137,7 +137,7 @@ std::string Rewriter::getRewritenText(SourceRange Range) const { // Adjust the end offset to the end of the last token, instead of being the // start of the last token. - EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr); + EndOff += Lexer::MeasureTokenLength(Range.getEnd(), *SourceMgr, *LangOpts); // Advance the iterators to the right spot, yay for linear time algorithms. RewriteBuffer::iterator Start = RB.begin(); diff --git a/tools/clang-cc/HTMLPrint.cpp b/tools/clang-cc/HTMLPrint.cpp index b2bb29c9eb..0e15973939 100644 --- a/tools/clang-cc/HTMLPrint.cpp +++ b/tools/clang-cc/HTMLPrint.cpp @@ -52,7 +52,7 @@ ASTConsumer* clang::CreateHTMLPrinter(const std::string &OutFile, } 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 3610da63c5..f1f53e41c5 100644 --- a/tools/clang-cc/RewriteBlocks.cpp +++ b/tools/clang-cc/RewriteBlocks.cpp @@ -198,7 +198,7 @@ void RewriteBlocks::Initialize(ASTContext &context) { 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 149a73c44b..d601f865b6 100644 --- a/tools/clang-cc/RewriteMacros.cpp +++ b/tools/clang-cc/RewriteMacros.cpp @@ -90,7 +90,7 @@ void clang::RewriteMacrosInInput(Preprocessor &PP,const std::string &InFileName, SourceManager &SM = PP.getSourceManager(); Rewriter Rewrite; - Rewrite.setSourceMgr(SM); + Rewrite.setSourceMgr(SM, PP.getLangOptions()); RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID()); std::vector RawTokens; diff --git a/tools/clang-cc/RewriteObjC.cpp b/tools/clang-cc/RewriteObjC.cpp index 44c0eb3d98..41f532b287 100644 --- a/tools/clang-cc/RewriteObjC.cpp +++ b/tools/clang-cc/RewriteObjC.cpp @@ -455,7 +455,7 @@ void RewriteObjC::Initialize(ASTContext &context) { 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 @@ void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, // 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 @@ void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, 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 @@ void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, // 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 3360649475..345a421585 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -387,6 +387,33 @@ static llvm::cl::opt LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), llvm::cl::Hidden); +static llvm::cl::opt +ObjCExclusiveGC("fobjc-gc-only", + llvm::cl::desc("Use GC exclusively for Objective-C related " + "memory management")); + +static llvm::cl::opt +ObjCEnableGC("fobjc-gc", + llvm::cl::desc("Enable Objective-C garbage collection")); + +static llvm::cl::opt +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 +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 @@ static void InitializeLangOptions(LangOptions &Options, LangKind LK){ 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 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, Options.setMainFileName(MainFileName.c_str()); } -static llvm::cl::opt -ObjCExclusiveGC("fobjc-gc-only", - llvm::cl::desc("Use GC exclusively for Objective-C related " - "memory management")); - -static llvm::cl::opt -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 -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 -OverflowChecking("ftrapv", - llvm::cl::desc("Trap on integer overflow"), - llvm::cl::init(false)); - //===----------------------------------------------------------------------===// // Target Triple Processing. //===----------------------------------------------------------------------===// @@ -2021,7 +2023,8 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, llvm::TimeRegion Timer(ClangFrontendTimer); Consumer.reset(new ASTConsumer()); FixItRewrite = new FixItRewriter(PP.getDiagnostics(), - PP.getSourceManager()); + PP.getSourceManager(), + PP.getLangOptions()); break; } @@ -2034,7 +2037,8 @@ static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, // 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 @@ int main(int argc, char **argv) { 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 @@ int main(int argc, char **argv) { llvm::OwningPtr 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 @@ int main(int argc, char **argv) { // Initialize language options, inferring file types from input filenames. LangOptions LangInfo; + + if (!VerifyDiagnostics) + static_cast(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.