data: Macro handling

Macros have now edges from Files where they are usesd
This commit is contained in:
Andreas Stallinger
2015-09-28 15:25:52 +02:00
parent dd4d74a1d9
commit e3696df5e1
19 changed files with 272 additions and 69 deletions
+36
View File
@@ -664,6 +664,42 @@ Id Storage::onFileIncludeParsed(const ParseLocation& location, const FileInfo& f
return fileNodeId;
}
Id Storage::onMacroDefineParsed(const ParseLocation &location, const NameHierarchy& macroNameHierarchy) {
Id macroId = 0;
Id fileId = m_sqliteStorage.getFileByName(location.filePath.fileName()).id;
if(fileId != 0)
{
macroId = addNodeHierarchy(Node::NODE_MACRO, macroNameHierarchy);
addSourceLocation(macroId, location);
}
else
{
//TODO: What to do with this ones?
//LOG_ERROR("External MacroDefined :" + macroNameHierarchy.getFullName());
}
return macroId;
}
Id Storage::onMacroExpandParsed(const ParseLocation &location, const NameHierarchy& macroNameHierarchy) {
Id macroExpandId = addNodeHierarchy(Node::NODE_UNDEFINED, macroNameHierarchy);
Id fileId = m_sqliteStorage.getFileByName(location.filePath.fileName()).id;
if(fileId != 0)
{
addEdge(fileId, macroExpandId,Edge::EDGE_MACRO_USAGE, location);
}
else
{
//TODO: What to do with this ones?
//LOG_ERROR("External MacroExpand :" + macroNameHierarchy.getFullName());
}
return 0;
}
Id Storage::getIdForNodeWithName(const std::string& fullName) const // use name hierarchy here
{
std::vector<std::string> nameParts = utility::splitToVector(fullName, "::");
+3
View File
@@ -119,6 +119,9 @@ public:
virtual Id onFileIncludeParsed(
const ParseLocation& location, const FileInfo& fileInfo, const FileInfo& includedFileInfo);
virtual Id onMacroDefineParsed(const ParseLocation& location, const NameHierarchy& macroNameHierarchy);
virtual Id onMacroExpandParsed(const ParseLocation& location, const NameHierarchy& macroNameHierarchy);
// StorageAccess implementation
virtual Id getIdForNodeWithName(const std::string& fullName) const;
virtual Id getIdForEdgeWithName(const std::string& name) const;
+11
View File
@@ -49,6 +49,8 @@ Edge::EdgeType Edge::intToType(int value)
return EDGE_INCLUDE;
case 0x8000:
return EDGE_AGGREGATION;
case 0x10000:
return EDGE_MACRO_USAGE;
}
return EDGE_NONE;
@@ -214,7 +216,10 @@ std::string Edge::getTypeString(EdgeType type)
return "include";
case EDGE_AGGREGATION:
return "aggregation";
case EDGE_MACRO_USAGE:
return "macro_use";
}
return "";
}
@@ -358,6 +363,12 @@ bool Edge::checkType() const
break;
}
return true;
case EDGE_MACRO_USAGE:
if(!m_to->isType(Node::NODE_MACRO) || !m_from->isType(Node::NODE_FILE))
{
break;
}
return true;
}
LOG_ERROR_STREAM(
+2 -1
View File
@@ -36,7 +36,8 @@ public:
EDGE_INCLUDE = 0x4000,
EDGE_AGGREGATION = 0x8000
EDGE_AGGREGATION = 0x8000,
EDGE_MACRO_USAGE = 0x10000,
};
static int typeToInt(EdgeType type);
+6
View File
@@ -47,7 +47,10 @@ std::string Node::getTypeString(NodeType type)
return "template_parameter_type";
case NODE_FILE:
return "file";
case NODE_MACRO:
return "macro";
}
return "";
}
@@ -92,7 +95,10 @@ Node::NodeType Node::intToType(int value)
return NODE_TEMPLATE_PARAMETER_TYPE;
case 0x8000:
return NODE_FILE;
case 0x10000:
return NODE_MACRO;
}
return NODE_UNDEFINED;
}
+2 -1
View File
@@ -40,7 +40,8 @@ public:
NODE_TYPEDEF = 0x2000,
NODE_TEMPLATE_PARAMETER_TYPE = 0x4000,
NODE_FILE = 0x8000
NODE_FILE = 0x8000,
NODE_MACRO = 0x10000,
};
static std::string getTypeString(NodeType type);
+9
View File
@@ -6,8 +6,12 @@
#include "utility/types.h"
#include "data/name/NameHierarchy.h"
#include "utility/file/FileInfo.h"
#include "ParseLocation.h"
struct ParseFunction;
struct ParseLocation;
struct ParseTypeUsage;
@@ -132,6 +136,11 @@ public:
virtual Id onFileParsed(const FileInfo& fileInfo) = 0;
virtual Id onFileIncludeParsed(
const ParseLocation& location, const FileInfo& fileInfo, const FileInfo& includedFileInfo) = 0;
virtual Id onMacroDefineParsed(const ParseLocation& location, const NameHierarchy& macroNameHierarchy) = 0;
virtual Id onMacroExpandParsed(
const ParseLocation& location, const NameHierarchy& macroNameHierarchy) = 0;
};
#endif // PARSER_CLIENT_H
+5 -5
View File
@@ -18,11 +18,11 @@ public:
virtual ~ASTVisitor();
// Left for debugging purposes. Uncomment to see a colored ast-dump of the parsed file.
// virtual bool VisitTranslationUnitDecl(clang::TranslationUnitDecl* decl)
// {
// decl->dump();
// return true;
// }
// virtual bool VisitTranslationUnitDecl(clang::TranslationUnitDecl* decl)
// {
// decl->dump();
// return true;
// }
// RecursiveASTVisitor implementation
virtual bool VisitStmt(const clang::Stmt* statement); // avoid visiting
@@ -1,5 +1,10 @@
#include "data/parser/cxx/PreprocessorCallbacks.h"
#include "clang/Driver/Util.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Lex/MacroArgs.h"
#include "utility/file/FileManager.h"
#include "utility/file/FileRegister.h"
@@ -62,6 +67,56 @@ void PreprocessorCallbacks::InclusionDirective(
}
}
void PreprocessorCallbacks::MacroDefined( const clang::Token &MacroNameTok, const clang::MacroDirective *MD)
{
// ignore builtin macros
if(m_sourceManager.getSpellingLoc(MacroNameTok.getLocation()).printToString(m_sourceManager)[0] == '<')
{
return;
}
m_sourceManager.getSpellingLoc(MacroNameTok.getLocation()).dump(m_sourceManager);
NameHierarchy nameHierarchy;
nameHierarchy.push(std::make_shared<NameElement>(MacroNameTok.getIdentifierInfo()->getName().str()));
m_client->onMacroDefineParsed( getParseLocation(MacroNameTok), nameHierarchy);
}
void PreprocessorCallbacks::MacroExpands(
const clang::Token &MacroNameTok, const clang::MacroDefinition &MD,
clang::SourceRange Range, const clang::MacroArgs *Args
){
NameHierarchy nameHierarchy;
nameHierarchy.push(std::make_shared<NameElement>(MacroNameTok.getIdentifierInfo()->getName().str()));
m_client->onMacroExpandParsed( getParseLocation(MacroNameTok), nameHierarchy);
}
ParseLocation PreprocessorCallbacks::getParseLocation(const clang::Token &MacroNameTok) const
{
clang::SourceLocation location = MacroNameTok.getLocation();
return ParseLocation(
m_sourceManager.getFilename(location),
m_sourceManager.getSpellingLineNumber(location),
m_sourceManager.getSpellingColumnNumber(location),
m_sourceManager.getSpellingLineNumber(MacroNameTok.getEndLoc()),
m_sourceManager.getSpellingColumnNumber(MacroNameTok.getEndLoc()) - 1
);
}
ParseLocation PreprocessorCallbacks::getParseLocation(clang::MacroInfo *macroInfo) const
{
clang::SourceLocation location = macroInfo->getDefinitionLoc();
clang::SourceLocation endLocation = macroInfo->getDefinitionEndLoc();
return ParseLocation(
m_sourceManager.getFilename(location),
m_sourceManager.getSpellingLineNumber(location),
m_sourceManager.getSpellingColumnNumber(location),
m_sourceManager.getSpellingLineNumber(endLocation),
m_sourceManager.getSpellingColumnNumber(endLocation) - 1
);
}
ParseLocation PreprocessorCallbacks::getParseLocation(const clang::SourceRange& sourceRange) const
{
if (sourceRange.isInvalid())
@@ -2,7 +2,9 @@
#define PREPROCESSOR_CALLBACKS_H
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Token.h"
class FileRegister;
class ParserClient;
@@ -22,8 +24,17 @@ public:
clang::CharSourceRange fileNameRange, const clang::FileEntry* fileEntry, llvm::StringRef searchPath,
llvm::StringRef relativePath, const clang::Module* imported);
virtual void MacroDefined(const clang::Token &MacroNameTok, const clang::MacroDirective *MD );
virtual void MacroExpands(
const clang::Token &MacroNameTok, const clang::MacroDefinition &MD,
clang::SourceRange Range, const clang::MacroArgs *Args
);
private:
ParseLocation getParseLocation(const clang::SourceRange& sourceRange) const;
ParseLocation getParseLocation(const clang::Token& MacroNameToc) const;
ParseLocation getParseLocation(clang::MacroInfo *MacroNameToc) const;
const clang::SourceManager& m_sourceManager;
ParserClient* m_client;