Skip to content

Commit

Permalink
Use enum class instead of enum for Diagnostic message type
Browse files Browse the repository at this point in the history
  • Loading branch information
tytus-metrycki authored and XiaowenHu96 committed Jul 31, 2020
1 parent 7add457 commit a744446
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
17 changes: 9 additions & 8 deletions src/ErrorReport.h
Expand Up @@ -68,7 +68,7 @@ class DiagnosticMessage {

class Diagnostic {
public:
enum Type { ERROR, WARNING };
enum class Type { ERROR, WARNING };

Diagnostic(Type type, DiagnosticMessage primaryMessage, std::vector<DiagnosticMessage> additionalMessages)
: type(type), primaryMessage(std::move(primaryMessage)),
Expand All @@ -90,7 +90,7 @@ class Diagnostic {
}

void print(std::ostream& out) const {
out << (type == ERROR ? "Error: " : "Warning: ");
out << (type == Type::ERROR ? "Error: " : "Warning: ");
out << primaryMessage;
for (const DiagnosticMessage& additionalMessage : additionalMessages) {
out << additionalMessage;
Expand Down Expand Up @@ -119,10 +119,10 @@ class Diagnostic {
}
}

if (type == ERROR && other.getType() == WARNING) {
if (type == Type::ERROR && other.getType() == Type::WARNING) {
return true;
}
if (other.getType() == ERROR && type == WARNING) {
if (other.getType() == Type::ERROR && type == Type::WARNING) {
return false;
}

Expand Down Expand Up @@ -150,12 +150,12 @@ class ErrorReport {

unsigned getNumErrors() const {
return std::count_if(diagnostics.begin(), diagnostics.end(),
[](Diagnostic d) -> bool { return d.getType() == Diagnostic::ERROR; });
[](Diagnostic d) -> bool { return d.getType() == Diagnostic::Type::ERROR; });
}

unsigned getNumWarnings() const {
return std::count_if(diagnostics.begin(), diagnostics.end(),
[](Diagnostic d) -> bool { return d.getType() == Diagnostic::WARNING; });
[](Diagnostic d) -> bool { return d.getType() == Diagnostic::Type::WARNING; });
}

unsigned getNumIssues() const {
Expand All @@ -164,14 +164,15 @@ class ErrorReport {

/** Adds an error with the given message and location */
void addError(const std::string& message, SrcLocation location) {
diagnostics.insert(Diagnostic(Diagnostic::ERROR, DiagnosticMessage(message, std::move(location))));
diagnostics.insert(
Diagnostic(Diagnostic::Type::ERROR, DiagnosticMessage(message, std::move(location))));
}

/** Adds a warning with the given message and location */
void addWarning(const std::string& message, SrcLocation location) {
if (!nowarn) {
diagnostics.insert(
Diagnostic(Diagnostic::WARNING, DiagnosticMessage(message, std::move(location))));
Diagnostic(Diagnostic::Type::WARNING, DiagnosticMessage(message, std::move(location))));
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/ParserDriver.cpp
Expand Up @@ -100,7 +100,7 @@ void ParserDriver::addPragma(std::unique_ptr<AstPragma> p) {
void ParserDriver::addFunctorDeclaration(std::unique_ptr<AstFunctorDeclaration> f) {
const std::string& name = f->getName();
if (const AstFunctorDeclaration* prev = getFunctorDeclaration(*translationUnit->getProgram(), name)) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition of functor " + toString(name), f->getSrcLoc()),
{DiagnosticMessage("Previous definition", prev->getSrcLoc())});
translationUnit->getErrorReport().addDiagnostic(err);
Expand All @@ -112,7 +112,7 @@ void ParserDriver::addFunctorDeclaration(std::unique_ptr<AstFunctorDeclaration>
void ParserDriver::addRelation(std::unique_ptr<AstRelation> r) {
const auto& name = r->getQualifiedName();
if (AstRelation* prev = getRelation(*translationUnit->getProgram(), name)) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition of relation " + toString(name), r->getSrcLoc()),
{DiagnosticMessage("Previous definition", prev->getSrcLoc())});
translationUnit->getErrorReport().addDiagnostic(err);
Expand All @@ -125,7 +125,7 @@ void ParserDriver::addIO(std::unique_ptr<AstIO> d) {
if (d->getType() == AstIoType::printsize) {
for (const auto& cur : translationUnit->getProgram()->getIOs()) {
if (cur->getQualifiedName() == d->getQualifiedName() && cur->getType() == AstIoType::printsize) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition of printsize directives for relation " +
toString(d->getQualifiedName()),
d->getSrcLoc()),
Expand All @@ -141,7 +141,7 @@ void ParserDriver::addIO(std::unique_ptr<AstIO> d) {
void ParserDriver::addType(std::unique_ptr<AstType> type) {
const auto& name = type->getQualifiedName();
if (const AstType* prev = getType(*translationUnit->getProgram(), name)) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition of type " + toString(name), type->getSrcLoc()),
{DiagnosticMessage("Previous definition", prev->getSrcLoc())});
translationUnit->getErrorReport().addDiagnostic(err);
Expand Down Expand Up @@ -217,7 +217,8 @@ void ParserDriver::error(const SrcLocation& loc, const std::string& msg) {
translationUnit->getErrorReport().addError(msg, loc);
}
void ParserDriver::error(const std::string& msg) {
translationUnit->getErrorReport().addDiagnostic(Diagnostic(Diagnostic::ERROR, DiagnosticMessage(msg)));
translationUnit->getErrorReport().addDiagnostic(
Diagnostic(Diagnostic::Type::ERROR, DiagnosticMessage(msg)));
}

} // end of namespace souffle
4 changes: 2 additions & 2 deletions src/ast/transform/AstSemanticChecker.cpp
Expand Up @@ -231,7 +231,7 @@ AstSemanticCheckerImpl::AstSemanticCheckerImpl(AstTranslationUnit& tu) : tu(tu)
std::string negOrAgg = hasNegation ? "negation" : "aggregation";
messages.push_back(
DiagnosticMessage("has cyclic " + negOrAgg, foundLiteral->getSrcLoc()));
report.addDiagnostic(Diagnostic(Diagnostic::ERROR,
report.addDiagnostic(Diagnostic(Diagnostic::Type::ERROR,
DiagnosticMessage("Unable to stratify relation(s) {" + relationsListStr + "}"),
messages));
break;
Expand Down Expand Up @@ -1212,7 +1212,7 @@ bool AstExecutionPlanChecker::transform(AstTranslationUnit& translationUnit) {
if (version <= maxVersion) {
for (const auto& cur : clause->getExecutionPlan()->getOrders()) {
if (cur.first >= version) {
translationUnit.getErrorReport().addDiagnostic(Diagnostic(Diagnostic::ERROR,
translationUnit.getErrorReport().addDiagnostic(Diagnostic(Diagnostic::Type::ERROR,
DiagnosticMessage(
"execution plan for version " + std::to_string(cur.first),
cur.second->getSrcLoc()),
Expand Down
6 changes: 3 additions & 3 deletions src/ast/transform/ComponentInstantiationTransformer.cpp
Expand Up @@ -62,7 +62,7 @@ struct ComponentContent {
return (element->getQualifiedName() == type->getQualifiedName());
});
if (foundItem != types.end()) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage(
"Redefinition of type " + toString(type->getQualifiedName()), type->getSrcLoc()),
{DiagnosticMessage("Previous definition", (*foundItem)->getSrcLoc())});
Expand All @@ -78,7 +78,7 @@ struct ComponentContent {
return (element->getQualifiedName() == rel->getQualifiedName());
});
if (foundItem != relations.end()) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition of relation " + toString(rel->getQualifiedName()),
rel->getSrcLoc()),
{DiagnosticMessage("Previous definition", (*foundItem)->getSrcLoc())});
Expand All @@ -100,7 +100,7 @@ struct ComponentContent {
if (foundItem != ios.end()) {
auto type = (*foundItem)->getType();
if (type == newIO->getType() && newIO->getType() != AstIoType::output) {
Diagnostic err(Diagnostic::ERROR,
Diagnostic err(Diagnostic::Type::ERROR,
DiagnosticMessage("Redefinition I/O operation " + toString(newIO->getQualifiedName()),
newIO->getSrcLoc()),
{DiagnosticMessage("Previous definition", (*foundItem)->getSrcLoc())});
Expand Down

0 comments on commit a744446

Please sign in to comment.