forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding C++ compiler diagnostic messages to the database (Ericsson#403)
Adding diagnostic messages to the database. clang::DiagnosticConsumer is handling the diagnostic messages (compilation errors, warnings, notes, etc.) during C/C++ compilation. These diagnostic messages are collected and stored in the database.
- Loading branch information
Showing
6 changed files
with
117 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <llvm/ADT/SmallString.h> | ||
#include <cppparser/filelocutil.h> | ||
#include "diagnosticmessagehandler.h" | ||
|
||
namespace cc | ||
{ | ||
namespace parser | ||
{ | ||
|
||
DiagnosticMessageHandler::DiagnosticMessageHandler( | ||
SourceManager& srcMgr_, | ||
std::shared_ptr<odb::database> db_) | ||
: _srcMgr(srcMgr_), _db(db_) | ||
{ | ||
} | ||
|
||
void DiagnosticMessageHandler::HandleDiagnostic( | ||
clang::DiagnosticsEngine::Level diagLevel_, | ||
const clang::Diagnostic& info_) | ||
{ | ||
clang::DiagnosticConsumer::HandleDiagnostic(diagLevel_, info_); | ||
|
||
model::BuildLog buildLog; | ||
|
||
//--- Message type ---// | ||
|
||
switch (diagLevel_) | ||
{ | ||
case clang::DiagnosticsEngine::Note: | ||
buildLog.log.type = model::BuildLogMessage::Note; | ||
break; | ||
case clang::DiagnosticsEngine::Warning: | ||
buildLog.log.type = model::BuildLogMessage::Warning; | ||
break; | ||
case clang::DiagnosticsEngine::Error: | ||
buildLog.log.type = model::BuildLogMessage::Error; | ||
break; | ||
case clang::DiagnosticsEngine::Fatal: | ||
buildLog.log.type = model::BuildLogMessage::FatalError; | ||
break; | ||
} | ||
|
||
//--- Message content ---// | ||
|
||
llvm::SmallString<512> content; | ||
info_.FormatDiagnostic(content); | ||
buildLog.log.message = content.c_str(); | ||
|
||
//--- Message location ---// | ||
|
||
if (info_.getLocation().isValid() && info_.hasSourceManager()) | ||
{ | ||
FileLocUtil fileLocUtil(info_.getSourceManager()); | ||
const clang::SourceLocation& loc = info_.getLocation(); | ||
|
||
model::Range fileLoc; | ||
fileLocUtil.setRange(loc, loc, fileLoc); | ||
|
||
buildLog.location.range = fileLoc; | ||
buildLog.location.file = _srcMgr.getFile(fileLocUtil.getFilePath(loc)); | ||
} | ||
|
||
_messages.push_back(buildLog); | ||
} | ||
|
||
DiagnosticMessageHandler::~DiagnosticMessageHandler() | ||
{ | ||
util::OdbTransaction{_db}([this](){ | ||
for (model::BuildLog& message : _messages) | ||
_db->persist(message); | ||
}); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef CC_PARSER_DIAGNOSTICMESSAGEHANDLER_H | ||
#define CC_PARSER_DIAGNOSTICMESSAGEHANDLER_H | ||
|
||
#include <vector> | ||
#include <clang/Basic/Diagnostic.h> | ||
#include <model/buildlog-odb.hxx> | ||
#include <parser/sourcemanager.h> | ||
|
||
namespace cc | ||
{ | ||
namespace parser | ||
{ | ||
|
||
class DiagnosticMessageHandler : public clang::DiagnosticConsumer | ||
{ | ||
public: | ||
DiagnosticMessageHandler( | ||
SourceManager& srcMgr_, | ||
std::shared_ptr<odb::database> db_); | ||
~DiagnosticMessageHandler(); | ||
|
||
void HandleDiagnostic( | ||
clang::DiagnosticsEngine::Level diagLevel_, | ||
const clang::Diagnostic& info_) override; | ||
|
||
private: | ||
SourceManager& _srcMgr; | ||
std::vector<model::BuildLog> _messages; | ||
std::shared_ptr<odb::database> _db; | ||
}; | ||
|
||
} | ||
} | ||
|
||
#endif |