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/lib/Frontend/FixItRewriter.cpp b/lib/Frontend/FixItRewriter.cpp
index 6fde5da..1ed89d7 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 83c2ac5..752d41f 100644
--- a/lib/Frontend/HTMLDiagnostics.cpp
+++ b/lib/Frontend/HTMLDiagnostics.cpp
@@ -163,7 +163,7 @@
return; // FIXME: Emit a warning?
// Create a new rewriter to generate HTML.
- Rewriter R(const_cast<SourceManager&>(SMgr));
+ Rewriter R(const_cast<SourceManager&>(SMgr), PP->getLangOptions());
// Process the path.
unsigned n = D.size();
@@ -574,8 +574,8 @@
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 @@
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 bc754cb..b0b35e4 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -482,14 +482,15 @@
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 d87a8f1..7262484 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 @@
class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient {
std::vector<const PathDiagnostic*> 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 @@
};
} // 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<FileID> &V,
@@ -66,9 +68,9 @@
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 @@
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<SourceManager&>(*SM));
+ FullSourceLoc Loc(SM.getInstantiationLoc(L), const_cast<SourceManager&>(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) << "<dict>\n";
Indent(o, indent) << " <key>line</key><integer>"
@@ -98,19 +102,20 @@
Indent(o, indent) << "</dict>\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) << "<array>\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) << "</array>\n";
}
@@ -134,7 +139,9 @@
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) << "<dict>\n";
@@ -145,9 +152,9 @@
// FIXME: Eventually remove (DEPRECATED)
// Output the start and end locations.
Indent(o, indent) << "<key>start</key>\n";
- EmitLocation(o, SM, P.getStartLocation(), FM, indent);
+ EmitLocation(o, SM, LangOpts, P.getStartLocation(), FM, indent);
Indent(o, indent) << "<key>end</key>\n";
- EmitLocation(o, SM, P.getEndLocation(), FM, indent);
+ EmitLocation(o, SM, LangOpts, P.getEndLocation(), FM, indent);
// Emit edges.
Indent(o, indent) << "<key>edges</key>\n";
@@ -159,9 +166,9 @@
Indent(o, indent) << "<dict>\n";
++indent;
Indent(o, indent) << "<key>start</key>\n";
- EmitRange(o, SM, I->getStart().asRange(), FM, indent+1);
+ EmitRange(o, SM, LangOpts, I->getStart().asRange(), FM, indent+1);
Indent(o, indent) << "<key>end</key>\n";
- EmitRange(o, SM, I->getEnd().asRange(), FM, indent+1);
+ EmitRange(o, SM, LangOpts, I->getEnd().asRange(), FM, indent+1);
--indent;
Indent(o, indent) << "</dict>\n";
}
@@ -181,7 +188,9 @@
}
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) << "<dict>\n";
@@ -193,7 +202,7 @@
FullSourceLoc L = P.getLocation().asLocation();
Indent(o, indent) << "<key>location</key>\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 @@
Indent(o, indent) << "<key>ranges</key>\n";
Indent(o, indent) << "<array>\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) << "</array>\n";
}
@@ -226,7 +236,8 @@
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 @@
default:
break;
case PathDiagnosticPiece::Event:
- ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, indent);
+ ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, LangOpts,
+ indent);
break;
case PathDiagnosticPiece::Macro:
- ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, indent);
+ ReportMacro(o, cast<PathDiagnosticMacroPiece>(**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<PathDiagnosticControlFlowPiece>(P), FM, SM,
- indent);
+ LangOpts, indent);
break;
case PathDiagnosticPiece::Event:
- ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, indent);
+ ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, LangOpts,
+ indent);
break;
case PathDiagnosticPiece::Macro:
- ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, indent);
+ ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts,
+ indent);
break;
}
}
@@ -352,7 +368,7 @@
o << " <array>\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 << " </array>\n";
@@ -366,7 +382,7 @@
// Output the location of the bug.
o << " <key>location</key>\n";
- EmitLocation(o, SM, D->getLocation(), FM, 2);
+ EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2);
// Close up the entry.
o << " </dict>\n";
diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp
index f1555db..c472a14 100644
--- a/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -82,7 +82,7 @@
--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 @@
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) << '-'