[Basic] Remove source manager references from diag state points.

This is just wasted space, we don't support state points from multiple
source managers. Validate that there's no state when resetting the
source manager and use the 'global' reference to the sourcemanager
instead of the ones in the diag state.

llvm-svn: 292402
This commit is contained in:
Benjamin Kramer 2017-01-18 15:50:26 +00:00
parent 97c7cf1d5c
commit bc9ef590cb
2 changed files with 40 additions and 33 deletions

View File

@ -239,19 +239,9 @@ private:
/// modifications done through the command-line. /// modifications done through the command-line.
struct DiagStatePoint { struct DiagStatePoint {
DiagState *State; DiagState *State;
FullSourceLoc Loc; SourceLocation Loc;
DiagStatePoint(DiagState *State, FullSourceLoc Loc) DiagStatePoint(DiagState *State, SourceLocation Loc)
: State(State), Loc(Loc) { } : State(State), Loc(Loc) { }
bool operator<(const DiagStatePoint &RHS) const {
// If Loc is invalid it means it came from <command-line>, in which case
// we regard it as coming before any valid source location.
if (RHS.Loc.isInvalid())
return false;
if (Loc.isInvalid())
return true;
return Loc.isBeforeInTranslationUnitThan(RHS.Loc);
}
}; };
/// \brief A sorted vector of all DiagStatePoints representing changes in /// \brief A sorted vector of all DiagStatePoints representing changes in
@ -271,16 +261,7 @@ private:
return DiagStatePoints.back().State; return DiagStatePoints.back().State;
} }
void PushDiagStatePoint(DiagState *State, SourceLocation L) { void PushDiagStatePoint(DiagState *State, SourceLocation L);
FullSourceLoc Loc(L, getSourceManager());
// Make sure that DiagStatePoints is always sorted according to Loc.
assert(Loc.isValid() && "Adding invalid loc point");
assert(!DiagStatePoints.empty() &&
(DiagStatePoints.back().Loc.isInvalid() ||
DiagStatePoints.back().Loc.isBeforeInTranslationUnitThan(Loc)) &&
"Previous point loc comes after or is the same as new one");
DiagStatePoints.push_back(DiagStatePoint(State, Loc));
}
/// \brief Finds the DiagStatePoint that contains the diagnostic state of /// \brief Finds the DiagStatePoint that contains the diagnostic state of
/// the given source location. /// the given source location.
@ -390,7 +371,11 @@ public:
assert(SourceMgr && "SourceManager not set!"); assert(SourceMgr && "SourceManager not set!");
return *SourceMgr; return *SourceMgr;
} }
void setSourceManager(SourceManager *SrcMgr) { SourceMgr = SrcMgr; } void setSourceManager(SourceManager *SrcMgr) {
assert(DiagStatePoints.size() == 1 && DiagStatePoints[0].Loc.isInvalid() &&
"Leftover diag state from a different SourceManager.");
SourceMgr = SrcMgr;
}
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
// DiagnosticsEngine characterization methods, used by a client to customize // DiagnosticsEngine characterization methods, used by a client to customize

View File

@ -16,6 +16,7 @@
#include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/IdentifierTable.h" #include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/PartialDiagnostic.h" #include "clang/Basic/PartialDiagnostic.h"
#include "clang/Basic/SourceManager.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CrashRecoveryContext.h" #include "llvm/Support/CrashRecoveryContext.h"
@ -137,7 +138,7 @@ void DiagnosticsEngine::Reset() {
// Create a DiagState and DiagStatePoint representing diagnostic changes // Create a DiagState and DiagStatePoint representing diagnostic changes
// through command-line. // through command-line.
DiagStates.emplace_back(); DiagStates.emplace_back();
DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc())); DiagStatePoints.emplace_back(&DiagStates.back(), SourceLocation());
} }
void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1, void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
@ -157,6 +158,18 @@ void DiagnosticsEngine::ReportDelayed() {
DelayedDiagArg2.clear(); DelayedDiagArg2.clear();
} }
void DiagnosticsEngine::PushDiagStatePoint(DiagState *State,
SourceLocation Loc) {
// Make sure that DiagStatePoints is always sorted according to Loc.
assert(Loc.isValid() && "Adding invalid loc point");
assert(!DiagStatePoints.empty() &&
(DiagStatePoints.back().Loc.isInvalid() ||
getSourceManager().isBeforeInTranslationUnit(
DiagStatePoints.back().Loc, Loc)) &&
"Previous point loc comes after or is the same as new one");
DiagStatePoints.push_back(DiagStatePoint(State, Loc));
}
DiagnosticsEngine::DiagStatePointsTy::iterator DiagnosticsEngine::DiagStatePointsTy::iterator
DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const { DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
assert(!DiagStatePoints.empty()); assert(!DiagStatePoints.empty());
@ -171,11 +184,21 @@ DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
return DiagStatePoints.end() - 1; return DiagStatePoints.end() - 1;
DiagStatePointsTy::iterator Pos = DiagStatePoints.end(); DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; SourceLocation LastStateChangePos = DiagStatePoints.back().Loc;
if (LastStateChangePos.isValid() && if (LastStateChangePos.isValid() &&
Loc.isBeforeInTranslationUnitThan(LastStateChangePos)) Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(), Pos = std::upper_bound(
DiagStatePoint(nullptr, Loc)); DiagStatePoints.begin(), DiagStatePoints.end(),
DiagStatePoint(nullptr, Loc),
[this](const DiagStatePoint &LHS, const DiagStatePoint &RHS) {
// If Loc is invalid it means it came from <command-line>, in which
// case we regard it as coming before any valid source location.
if (RHS.Loc.isInvalid())
return false;
if (LHS.Loc.isInvalid())
return true;
return SourceMgr->isBeforeInTranslationUnit(LHS.Loc, RHS.Loc);
});
--Pos; --Pos;
return Pos; return Pos;
} }
@ -190,8 +213,8 @@ void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
assert(!DiagStatePoints.empty()); assert(!DiagStatePoints.empty());
assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location"); assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc(); SourceLocation Loc = SourceMgr ? L : SourceLocation();
FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc; SourceLocation LastStateChangePos = DiagStatePoints.back().Loc;
// Don't allow a mapping to a warning override an error/fatal mapping. // Don't allow a mapping to a warning override an error/fatal mapping.
if (Map == diag::Severity::Warning) { if (Map == diag::Severity::Warning) {
DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag); DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
@ -210,7 +233,7 @@ void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
// Another common case; modifying diagnostic state in a source location // Another common case; modifying diagnostic state in a source location
// after the previous one. // after the previous one.
if ((Loc.isValid() && LastStateChangePos.isInvalid()) || if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) { SourceMgr->isBeforeInTranslationUnit(LastStateChangePos, Loc)) {
// A diagnostic pragma occurred, create a new DiagState initialized with // A diagnostic pragma occurred, create a new DiagState initialized with
// the current one and a new DiagStatePoint to record at which location // the current one and a new DiagStatePoint to record at which location
// the new state became active. // the new state became active.
@ -240,12 +263,11 @@ void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
// Create a new state/point and fit it into the vector of DiagStatePoints // Create a new state/point and fit it into the vector of DiagStatePoints
// so that the vector is always ordered according to location. // so that the vector is always ordered according to location.
assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc)); assert(SourceMgr->isBeforeInTranslationUnit(Pos->Loc, Loc));
DiagStates.push_back(*Pos->State); DiagStates.push_back(*Pos->State);
DiagState *NewState = &DiagStates.back(); DiagState *NewState = &DiagStates.back();
NewState->setMapping(Diag, Mapping); NewState->setMapping(Diag, Mapping);
DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState, DiagStatePoints.insert(Pos + 1, DiagStatePoint(NewState, Loc));
FullSourceLoc(Loc, *SourceMgr)));
} }
bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor, bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,