Skip to content

Commit

Permalink
Improve diagnostic mapping terminology
Browse files Browse the repository at this point in the history
Diagnostic mappings are used to calculate the final severity of diagnostic
instances.

Detangle the implementation to reflect the terminology used in documentation
and bindings.

No change in functionality.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210518 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
atoker committed Jun 10, 2014
1 parent 5328ab0 commit e451a7e
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 100 deletions.
35 changes: 17 additions & 18 deletions include/clang/Basic/Diagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
/// \brief Mapping information for diagnostics.
///
/// Mapping info is packed into four bits per diagnostic. The low three
/// bits are the mapping (an instance of diag::Mapping), or zero if unset.
/// bits are the mapping (an instance of diag::Severity), or zero if unset.
/// The high bit is set when the mapping was established as a user mapping.
/// If the high bit is clear, then the low bits are set to the default
/// value, and should be mapped with -pedantic, -Werror, etc.
Expand All @@ -209,19 +209,18 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
/// the state so that we know what is the diagnostic state at any given
/// source location.
class DiagState {
llvm::DenseMap<unsigned, DiagnosticMappingInfo> DiagMap;
llvm::DenseMap<unsigned, DiagnosticMapping> DiagMap;

public:
typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::iterator
iterator;
typedef llvm::DenseMap<unsigned, DiagnosticMappingInfo>::const_iterator
const_iterator;
typedef llvm::DenseMap<unsigned, DiagnosticMapping>::iterator iterator;
typedef llvm::DenseMap<unsigned, DiagnosticMapping>::const_iterator
const_iterator;

void setMappingInfo(diag::kind Diag, DiagnosticMappingInfo Info) {
void setMapping(diag::kind Diag, DiagnosticMapping Info) {
DiagMap[Diag] = Info;
}

DiagnosticMappingInfo &getOrAddMappingInfo(diag::kind Diag);
DiagnosticMapping &getOrAddMapping(diag::kind Diag);

const_iterator begin() const { return DiagMap.begin(); }
const_iterator end() const { return DiagMap.end(); }
Expand Down Expand Up @@ -547,7 +546,7 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
///
/// \param Loc The source location that this change of diagnostic state should
/// take affect. It can be null if we are setting the latest state.
void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
void setDiagnosticMapping(diag::kind Diag, diag::Severity Map,
SourceLocation Loc);

/// \brief Change an entire diagnostic group (e.g. "unknown-pragmas") to
Expand All @@ -558,7 +557,7 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
///
/// \param Loc The source location that this change of diagnostic state should
/// take affect. It can be null if we are setting the state from command-line.
bool setDiagnosticGroupMapping(StringRef Group, diag::Mapping Map,
bool setDiagnosticGroupMapping(StringRef Group, diag::Severity Map,
SourceLocation Loc = SourceLocation());

/// \brief Set the warning-as-error flag for the given diagnostic group.
Expand All @@ -579,8 +578,8 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
///
/// Mainly to be used by -Wno-everything to disable all warnings but allow
/// subsequent -W options to enable specific warnings.
void setMappingToAllDiagnostics(diag::Mapping Map,
SourceLocation Loc = SourceLocation());
void setMappingForAllDiagnostics(diag::Severity Map,
SourceLocation Loc = SourceLocation());

bool hasErrorOccurred() const { return ErrorOccurred; }

Expand Down Expand Up @@ -770,19 +769,19 @@ class DiagnosticsEngine : public RefCountedBase<DiagnosticsEngine> {
/// or modify at a particular position.
SmallVector<FixItHint, 8> DiagFixItHints;

DiagnosticMappingInfo makeMappingInfo(diag::Mapping Map, SourceLocation L) {
DiagnosticMapping makeUserMapping(diag::Severity Map, SourceLocation L) {
bool isPragma = L.isValid();
DiagnosticMappingInfo MappingInfo = DiagnosticMappingInfo::Make(
Map, /*IsUser=*/true, isPragma);
DiagnosticMapping Mapping =
DiagnosticMapping::Make(Map, /*IsUser=*/true, isPragma);

// If this is a pragma mapping, then set the diagnostic mapping flags so
// that we override command line options.
if (isPragma) {
MappingInfo.setNoWarningAsError(true);
MappingInfo.setNoErrorAsFatal(true);
Mapping.setNoWarningAsError(true);
Mapping.setNoErrorAsFatal(true);
}

return MappingInfo;
return Mapping;
}

/// \brief Used to report a diagnostic that is finally fully formed.
Expand Down
28 changes: 14 additions & 14 deletions include/clang/Basic/DiagnosticIDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,37 @@ namespace clang {
/// (emit a warning), MAP_ERROR (emit as an error). It allows clients to
/// map errors to MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting
/// diagnostics after this one).
enum Mapping {
enum Severity {
// NOTE: 0 means "uncomputed".
MAP_IGNORE = 1, ///< Map this diagnostic to nothing, ignore it.
MAP_REMARK = 2, ///< Map this diagnostic to a remark.
MAP_WARNING = 3, ///< Map this diagnostic to a warning.
MAP_ERROR = 4, ///< Map this diagnostic to an error.
MAP_FATAL = 5 ///< Map this diagnostic to a fatal error.
MAP_IGNORE = 1, ///< Map this diagnostic to nothing, ignore it.
MAP_REMARK = 2, ///< Map this diagnostic to a remark.
MAP_WARNING = 3, ///< Map this diagnostic to a warning.
MAP_ERROR = 4, ///< Map this diagnostic to an error.
MAP_FATAL = 5 ///< Map this diagnostic to a fatal error.
};
}

class DiagnosticMappingInfo {
unsigned Mapping : 3;
class DiagnosticMapping {
unsigned Severity : 3;
unsigned IsUser : 1;
unsigned IsPragma : 1;
unsigned HasNoWarningAsError : 1;
unsigned HasNoErrorAsFatal : 1;

public:
static DiagnosticMappingInfo Make(diag::Mapping Mapping, bool IsUser,
bool IsPragma) {
DiagnosticMappingInfo Result;
Result.Mapping = Mapping;
static DiagnosticMapping Make(diag::Severity Severity, bool IsUser,
bool IsPragma) {
DiagnosticMapping Result;
Result.Severity = Severity;
Result.IsUser = IsUser;
Result.IsPragma = IsPragma;
Result.HasNoWarningAsError = 0;
Result.HasNoErrorAsFatal = 0;
return Result;
}

diag::Mapping getMapping() const { return diag::Mapping(Mapping); }
void setMapping(diag::Mapping Value) { Mapping = Value; }
diag::Severity getSeverity() const { return diag::Severity(Severity); }
void setSeverity(diag::Severity Value) { Severity = Value; }

bool isUser() const { return IsUser; }
bool isPragma() const { return IsPragma; }
Expand Down
5 changes: 2 additions & 3 deletions include/clang/Lex/PPCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ class PPCallbacks {

/// \brief Callback invoked when a \#pragma gcc dianostic directive is read.
virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
diag::Mapping mapping, StringRef Str) {
}
diag::Severity mapping, StringRef Str) {}

/// \brief Called when an OpenCL extension is either disabled or
/// enabled with a pragma.
Expand Down Expand Up @@ -411,7 +410,7 @@ class PPChainedCallbacks : public PPCallbacks {
}

void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
diag::Mapping mapping, StringRef Str) override {
diag::Severity mapping, StringRef Str) override {
First->PragmaDiagnostic(Loc, Namespace, mapping, Str);
Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
}
Expand Down
49 changes: 24 additions & 25 deletions lib/Basic/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
return Pos;
}

void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag,
diag::Severity Map,
SourceLocation L) {
assert(Diag < diag::DIAG_UPPER_LIMIT &&
"Can only map builtin diagnostics");
Expand All @@ -176,16 +177,16 @@ void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
// Don't allow a mapping to a warning override an error/fatal mapping.
if (Map == diag::MAP_WARNING) {
DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(Diag);
if (Info.getMapping() == diag::MAP_ERROR ||
Info.getMapping() == diag::MAP_FATAL)
Map = Info.getMapping();
DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
if (Info.getSeverity() == diag::MAP_ERROR ||
Info.getSeverity() == diag::MAP_FATAL)
Map = Info.getSeverity();
}
DiagnosticMappingInfo MappingInfo = makeMappingInfo(Map, L);
DiagnosticMapping Mapping = makeUserMapping(Map, L);

// Common case; setting all the diagnostics of a group in one place.
if (Loc.isInvalid() || Loc == LastStateChangePos) {
GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
GetCurDiagState()->setMapping(Diag, Mapping);
return;
}

Expand All @@ -198,7 +199,7 @@ void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
// the new state became active.
DiagStates.push_back(*GetCurDiagState());
PushDiagStatePoint(&DiagStates.back(), Loc);
GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
GetCurDiagState()->setMapping(Diag, Mapping);
return;
}

Expand All @@ -211,12 +212,12 @@ void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
// Update all diagnostic states that are active after the given location.
for (DiagStatePointsTy::iterator
I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
GetCurDiagState()->setMapping(Diag, Mapping);
}

// If the location corresponds to an existing point, just update its state.
if (Pos->Loc == Loc) {
GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
GetCurDiagState()->setMapping(Diag, Mapping);
return;
}

Expand All @@ -225,14 +226,14 @@ void DiagnosticsEngine::setDiagnosticMapping(diag::kind Diag, diag::Mapping Map,
assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
DiagStates.push_back(*Pos->State);
DiagState *NewState = &DiagStates.back();
GetCurDiagState()->setMappingInfo(Diag, MappingInfo);
GetCurDiagState()->setMapping(Diag, Mapping);
DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
FullSourceLoc(Loc, *SourceMgr)));
}

bool DiagnosticsEngine::setDiagnosticGroupMapping(
StringRef Group, diag::Mapping Map, SourceLocation Loc)
{
bool DiagnosticsEngine::setDiagnosticGroupMapping(StringRef Group,
diag::Severity Map,
SourceLocation Loc) {
// Get the diagnostics in this group.
SmallVector<diag::kind, 8> GroupDiags;
if (Diags->getDiagnosticsInGroup(Group, GroupDiags))
Expand Down Expand Up @@ -262,12 +263,11 @@ bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,

// Perform the mapping change.
for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
GroupDiags[i]);
DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);

if (Info.getMapping() == diag::MAP_ERROR ||
Info.getMapping() == diag::MAP_FATAL)
Info.setMapping(diag::MAP_WARNING);
if (Info.getSeverity() == diag::MAP_ERROR ||
Info.getSeverity() == diag::MAP_FATAL)
Info.setSeverity(diag::MAP_WARNING);

Info.setNoWarningAsError(true);
}
Expand All @@ -292,20 +292,19 @@ bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,

// Perform the mapping change.
for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
DiagnosticMappingInfo &Info = GetCurDiagState()->getOrAddMappingInfo(
GroupDiags[i]);
DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);

if (Info.getMapping() == diag::MAP_FATAL)
Info.setMapping(diag::MAP_ERROR);
if (Info.getSeverity() == diag::MAP_FATAL)
Info.setSeverity(diag::MAP_ERROR);

Info.setNoErrorAsFatal(true);
}

return false;
}

void DiagnosticsEngine::setMappingToAllDiagnostics(diag::Mapping Map,
SourceLocation Loc) {
void DiagnosticsEngine::setMappingForAllDiagnostics(diag::Severity Map,
SourceLocation Loc) {
// Get all the diagnostics.
SmallVector<diag::kind, 64> AllDiags;
Diags->getAllDiagnostics(AllDiags);
Expand Down
Loading

0 comments on commit e451a7e

Please sign in to comment.