ui/logic/data: show parse errors in CodeView
This change saves the errors while parsing in the Storage and displays them in the CodeView. The error messages can be read in the tooltip on hovering. The error count is visible in the status bar.
This commit is contained in:
@@ -48,6 +48,30 @@ void Storage::logLocations() const
|
||||
LOG_INFO_STREAM(<< '\n' << m_locationCollection);
|
||||
}
|
||||
|
||||
size_t Storage::getErrorCount() const
|
||||
{
|
||||
return m_errorMessages.size();
|
||||
}
|
||||
|
||||
void Storage::onError(const ParseLocation& location, const std::string& message)
|
||||
{
|
||||
log("ERROR", message, location);
|
||||
|
||||
if (!location.isValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Id errorId = m_errorMessages.size();
|
||||
|
||||
TokenLocation* loc = m_errorLocationCollection.addTokenLocation(
|
||||
errorId, location.filePath,
|
||||
location.startLineNumber, location.startColumnNumber,
|
||||
location.endLineNumber, location.endColumnNumber
|
||||
);
|
||||
|
||||
m_errorMessages.push_back(message);
|
||||
}
|
||||
|
||||
Id Storage::onTypedefParsed(
|
||||
const ParseLocation& location, const std::vector<std::string>& nameHierarchy, const ParseTypeUsage& underlyingType,
|
||||
@@ -576,7 +600,6 @@ std::vector<Id> Storage::getActiveTokenIdsForId(Id tokenId, Id* declarationId) c
|
||||
|
||||
ret.push_back(token->getId());
|
||||
|
||||
Node* node;
|
||||
if (token->isNode())
|
||||
{
|
||||
Node* node = dynamic_cast<Node*>(token);
|
||||
@@ -744,6 +767,13 @@ TokenLocationFile Storage::getTokenLocationsForLinesInFile(
|
||||
return ret;
|
||||
}
|
||||
|
||||
TokenLocationCollection Storage::getErrorTokenLocations(std::vector<std::string>* errorMessages) const
|
||||
{
|
||||
errorMessages->insert(errorMessages->begin(), m_errorMessages.begin(), m_errorMessages.end());
|
||||
|
||||
return m_errorLocationCollection;
|
||||
}
|
||||
|
||||
const Graph& Storage::getGraph() const
|
||||
{
|
||||
return m_graph;
|
||||
@@ -941,8 +971,16 @@ bool Storage::getSubQuerySearchResults(
|
||||
{
|
||||
if (word.size())
|
||||
{
|
||||
SearchResults res = node->runFuzzySearch(word);
|
||||
results->insert(res.begin(), res.end());
|
||||
if (searchNodes.size() > 1)
|
||||
{
|
||||
SearchResults res = node->runFuzzySearchOnSelf(word);
|
||||
results->insert(res.begin(), res.end());
|
||||
}
|
||||
else
|
||||
{
|
||||
SearchResults res = node->runFuzzySearch(word);
|
||||
results->insert(res.begin(), res.end());
|
||||
}
|
||||
}
|
||||
else if (searchNodes.size() == 1)
|
||||
{
|
||||
|
||||
@@ -27,7 +27,11 @@ public:
|
||||
void logGraph() const;
|
||||
void logLocations() const;
|
||||
|
||||
size_t getErrorCount() const;
|
||||
|
||||
// ParserClient implementation
|
||||
virtual void onError(const ParseLocation& location, const std::string& message);
|
||||
|
||||
virtual Id onTypedefParsed(
|
||||
const ParseLocation& location, const std::vector<std::string>& nameHierarchy,
|
||||
const ParseTypeUsage& underlyingType, AccessType access);
|
||||
@@ -108,6 +112,8 @@ public:
|
||||
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
|
||||
) const;
|
||||
|
||||
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const;
|
||||
|
||||
protected:
|
||||
const Graph& getGraph() const;
|
||||
const TokenLocationCollection& getTokenLocationCollection() const;
|
||||
@@ -137,6 +143,9 @@ private:
|
||||
|
||||
SearchIndex m_tokenIndex;
|
||||
SearchIndex m_filterIndex;
|
||||
|
||||
TokenLocationCollection m_errorLocationCollection;
|
||||
std::vector<std::string> m_errorMessages;
|
||||
};
|
||||
|
||||
#endif // STORAGE_H
|
||||
|
||||
@@ -18,6 +18,8 @@ public:
|
||||
virtual TokenLocationFile getTokenLocationsForLinesInFile(
|
||||
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
|
||||
) const = 0;
|
||||
|
||||
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const = 0;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -60,3 +60,13 @@ TokenLocationFile LocationAccessProxy::getTokenLocationsForLinesInFile(
|
||||
|
||||
return TokenLocationFile("");
|
||||
}
|
||||
|
||||
TokenLocationCollection LocationAccessProxy::getErrorTokenLocations(std::vector<std::string>* errorMessages) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getErrorTokenLocations(errorMessages);
|
||||
}
|
||||
|
||||
return TokenLocationCollection();
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ public:
|
||||
const std::string& filePath, uint firstLineNumber, uint lastLineNumber
|
||||
) const;
|
||||
|
||||
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const;
|
||||
|
||||
private:
|
||||
LocationAccess* m_subject;
|
||||
};
|
||||
|
||||
@@ -9,6 +9,19 @@ ParseLocation::ParseLocation()
|
||||
{
|
||||
}
|
||||
|
||||
ParseLocation::ParseLocation(
|
||||
const std::string& filePath,
|
||||
uint lineNumber,
|
||||
uint columnNumber
|
||||
)
|
||||
: filePath(filePath)
|
||||
, startLineNumber(lineNumber)
|
||||
, startColumnNumber(columnNumber)
|
||||
, endLineNumber(lineNumber)
|
||||
, endColumnNumber(columnNumber)
|
||||
{
|
||||
}
|
||||
|
||||
ParseLocation::ParseLocation(
|
||||
const std::string& filePath,
|
||||
uint startLineNumber, uint startColumnNumber,
|
||||
|
||||
@@ -8,6 +8,11 @@
|
||||
struct ParseLocation
|
||||
{
|
||||
ParseLocation();
|
||||
ParseLocation(
|
||||
const std::string& filePath,
|
||||
uint lineNumber,
|
||||
uint columnNumber
|
||||
);
|
||||
ParseLocation(
|
||||
const std::string& filePath,
|
||||
uint startLineNumber, uint startColumnNumber,
|
||||
|
||||
@@ -49,6 +49,8 @@ public:
|
||||
ParserClient();
|
||||
virtual ~ParserClient();
|
||||
|
||||
virtual void onError(const ParseLocation& location, const std::string& message) = 0;
|
||||
|
||||
virtual Id onTypedefParsed(
|
||||
const ParseLocation& location, const std::vector<std::string>& nameHierarchy,
|
||||
const ParseTypeUsage& underlyingType, AccessType access) = 0;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "data/parser/cxx/ASTConsumer.h"
|
||||
|
||||
#include "data/parser/ParserClient.h"
|
||||
|
||||
ASTConsumer::ASTConsumer(clang::ASTContext* context, ParserClient* client)
|
||||
: m_visitor(context, client)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
#include "data/parser/cxx/CxxDiagnosticConsumer.h"
|
||||
|
||||
#include "clang/Basic/SourceManager.h"
|
||||
|
||||
#include "data/parser/ParseLocation.h"
|
||||
#include "data/parser/ParserClient.h"
|
||||
|
||||
CxxDiagnosticConsumer::CxxDiagnosticConsumer(
|
||||
clang::raw_ostream &os,
|
||||
clang::DiagnosticOptions *diags,
|
||||
ParserClient* client,
|
||||
bool useLogging
|
||||
)
|
||||
: clang::TextDiagnosticPrinter(os, diags)
|
||||
, m_client(client)
|
||||
, m_isParsingFile(false)
|
||||
, m_useLogging(useLogging)
|
||||
{
|
||||
}
|
||||
|
||||
void CxxDiagnosticConsumer::BeginSourceFile(const clang::LangOptions& langOptions, const clang::Preprocessor* preProcessor)
|
||||
{
|
||||
if (m_useLogging)
|
||||
{
|
||||
clang::TextDiagnosticPrinter::BeginSourceFile(langOptions, preProcessor);
|
||||
}
|
||||
|
||||
m_isParsingFile = true;
|
||||
}
|
||||
|
||||
void CxxDiagnosticConsumer::EndSourceFile()
|
||||
{
|
||||
if (m_useLogging)
|
||||
{
|
||||
clang::TextDiagnosticPrinter::EndSourceFile();
|
||||
}
|
||||
|
||||
m_isParsingFile = false;
|
||||
}
|
||||
|
||||
void CxxDiagnosticConsumer::HandleDiagnostic(clang::DiagnosticsEngine::Level level, const clang::Diagnostic& info)
|
||||
{
|
||||
if (m_useLogging)
|
||||
{
|
||||
clang::TextDiagnosticPrinter::HandleDiagnostic(level, info);
|
||||
}
|
||||
|
||||
if (!m_isParsingFile)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (level == clang::DiagnosticsEngine::Error || level == clang::DiagnosticsEngine::Fatal)
|
||||
{
|
||||
llvm::SmallString<100> messageStr;
|
||||
info.FormatDiagnostic(messageStr);
|
||||
std::string message = messageStr.str();
|
||||
|
||||
std::string filePath;
|
||||
uint line = 0;
|
||||
uint column = 0;
|
||||
|
||||
if (info.getLocation().isValid() && info.hasSourceManager())
|
||||
{
|
||||
const clang::SourceManager& sourceManager = info.getSourceManager();
|
||||
clang::PresumedLoc presumedLocation = sourceManager.getPresumedLoc(info.getLocation());
|
||||
|
||||
filePath = presumedLocation.getFilename();
|
||||
line = presumedLocation.getLine();
|
||||
column = presumedLocation.getColumn();
|
||||
}
|
||||
|
||||
m_client->onError(ParseLocation(filePath, line, column), message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef CXX_DIAGNOSTIC_CONSUMER
|
||||
#define CXX_DIAGNOSTIC_CONSUMER
|
||||
|
||||
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
||||
|
||||
class ParserClient;
|
||||
|
||||
class CxxDiagnosticConsumer
|
||||
: public clang::TextDiagnosticPrinter
|
||||
{
|
||||
public:
|
||||
CxxDiagnosticConsumer(clang::raw_ostream &os, clang::DiagnosticOptions *diags, ParserClient* client, bool useLogging = true);
|
||||
|
||||
void BeginSourceFile(const clang::LangOptions& langOptions, const clang::Preprocessor* preProcessor);
|
||||
void EndSourceFile();
|
||||
|
||||
void HandleDiagnostic(clang::DiagnosticsEngine::Level level, const clang::Diagnostic& info);
|
||||
|
||||
private:
|
||||
ParserClient* m_client;
|
||||
bool m_isParsingFile;
|
||||
bool m_useLogging;
|
||||
};
|
||||
|
||||
#endif // CXX_DIAGNOSTIC_CONSUMER
|
||||
@@ -1,9 +1,54 @@
|
||||
#include "data/parser/cxx/CxxParser.h"
|
||||
|
||||
#include "data/parser/cxx/ASTActionFactory.h"
|
||||
#include "utility/logging/logging.h"
|
||||
#include "utility/text/TextAccess.h"
|
||||
|
||||
#include "data/parser/cxx/ASTActionFactory.h"
|
||||
#include "data/parser/cxx/CxxDiagnosticConsumer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
static std::vector<std::string> getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, llvm::StringRef FileName)
|
||||
{
|
||||
std::vector<std::string> Args;
|
||||
Args.push_back("clang-tool");
|
||||
Args.push_back("-fsyntax-only");
|
||||
Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
|
||||
Args.push_back(FileName.str());
|
||||
return Args;
|
||||
}
|
||||
|
||||
// custom implementation of clang::runToolOnCodeWithArgs which also sets our custon DiagnosticConsumer
|
||||
static bool runToolOnCodeWithArgs(
|
||||
clang::DiagnosticConsumer* DiagConsumer,
|
||||
clang::FrontendAction *ToolAction,
|
||||
const llvm::Twine &Code,
|
||||
const std::vector<std::string> &Args,
|
||||
const llvm::Twine &FileName = "input.cc",
|
||||
const clang::tooling::FileContentMappings &VirtualMappedFiles = clang::tooling::FileContentMappings()
|
||||
){
|
||||
llvm::SmallString<16> FileNameStorage;
|
||||
llvm::StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
|
||||
llvm::IntrusiveRefCntPtr<clang::FileManager> Files(new clang::FileManager(clang::FileSystemOptions()));
|
||||
clang::tooling::ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction, Files.get());
|
||||
|
||||
llvm::SmallString<1024> CodeStorage;
|
||||
Invocation.mapVirtualFile(FileNameRef,
|
||||
Code.toNullTerminatedStringRef(CodeStorage));
|
||||
|
||||
for (auto &FilenameWithContent : VirtualMappedFiles)
|
||||
{
|
||||
Invocation.mapVirtualFile(FilenameWithContent.first,
|
||||
FilenameWithContent.second);
|
||||
}
|
||||
|
||||
Invocation.setDiagnosticConsumer(DiagConsumer);
|
||||
|
||||
return Invocation.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CxxParser::CxxParser(ParserClient* client)
|
||||
: Parser(client)
|
||||
{
|
||||
@@ -68,15 +113,22 @@ void CxxParser::parseFiles(
|
||||
|
||||
clang::tooling::ClangTool tool(*compilationDatabase, filePaths);
|
||||
|
||||
ASTActionFactory actionFactory(m_client);
|
||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> options = new clang::DiagnosticOptions();
|
||||
CxxDiagnosticConsumer reporter(llvm::errs(), &*options, m_client);
|
||||
tool.setDiagnosticConsumer(&reporter);
|
||||
|
||||
ASTActionFactory actionFactory(m_client);
|
||||
tool.run(&actionFactory);
|
||||
}
|
||||
|
||||
void CxxParser::parseFile(std::shared_ptr<TextAccess> textAccess)
|
||||
{
|
||||
ASTActionFactory actionFactory(m_client);
|
||||
std::vector<std::string> args;
|
||||
args.push_back("-fno-delayed-template-parsing");
|
||||
clang::tooling::runToolOnCodeWithArgs(actionFactory.create(), textAccess->getText(), args);
|
||||
|
||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> options = new clang::DiagnosticOptions();
|
||||
CxxDiagnosticConsumer reporter(llvm::errs(), &*options, m_client, false);
|
||||
|
||||
ASTActionFactory actionFactory(m_client);
|
||||
runToolOnCodeWithArgs(&reporter, actionFactory.create(), textAccess->getText(), args);
|
||||
}
|
||||
|
||||
@@ -107,6 +107,20 @@ SearchResults SearchNode::runFuzzySearch(const std::string& query) const
|
||||
return result;
|
||||
}
|
||||
|
||||
SearchResults SearchNode::runFuzzySearchOnSelf(const std::string& query) const
|
||||
{
|
||||
SearchResults result;
|
||||
FuzzyMap m = fuzzyMatchRecursive(query, 0, 0, 0);
|
||||
for (const std::pair<size_t, const SearchNode*>& p : m)
|
||||
{
|
||||
addResultsRecursive(result, p.first, p.second);
|
||||
}
|
||||
|
||||
// TODO: Currently all matches are added to the ordered set and get compared by their fullName for alphabetical
|
||||
// order. This could be improved by limiting the number of items to e.g. 100.
|
||||
return result;
|
||||
}
|
||||
|
||||
void SearchNode::addResultsRecursive(SearchResults& result, size_t weight, const SearchNode* node) const
|
||||
{
|
||||
result.insert(SearchResult(weight, node, this));
|
||||
|
||||
@@ -37,6 +37,8 @@ public:
|
||||
const std::set<std::shared_ptr<SearchNode>>& getChildren() const;
|
||||
|
||||
SearchResults runFuzzySearch(const std::string& query) const;
|
||||
SearchResults runFuzzySearchOnSelf(const std::string& query) const;
|
||||
|
||||
void addResultsRecursive(SearchResults& result, size_t weight, const SearchNode* node) const;
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user