logic: add filename info to static global variables (issue #514)

This commit is contained in:
mlangkabel
2017-11-09 17:29:59 +01:00
parent ead9f9a634
commit e7f9940582
14 changed files with 168 additions and 98 deletions
@@ -8,6 +8,7 @@
#include "data/parser/cxx/name_resolver/CxxSpecifierNameResolver.h"
#include "data/parser/cxx/name_resolver/CxxTemplateArgumentNameResolver.h"
#include "data/parser/cxx/name_resolver/CxxTypeNameResolver.h"
#include "data/parser/cxx/utilityClang.h"
#include "utility/file/FilePath.h"
#include "utility/ScopedSwitcher.h"
@@ -327,7 +328,40 @@ std::shared_ptr<CxxDeclName> CxxDeclNameResolver::getDeclName(const clang::Named
CxxTypeNameResolver typenNameResolver(getIgnoredContextDecls());
typenNameResolver.ignoreContextDecl(varDecl);
std::shared_ptr<CxxTypeName> typeName = CxxTypeName::makeUnsolvedIfNull(typenNameResolver.getName(varDecl->getType()));
return std::make_shared<CxxVariableDeclName>(declNameString, std::vector<std::string>(), typeName, isStatic);
std::string varName = declNameString;
if (utility::getSymbolKind(varDecl) == SYMBOL_GLOBAL_VARIABLE &&
varDecl->getStorageClass() == clang::SC_Static)
{
// if a global variable is static it is only visible in the current translation unit. Therefore if multiple instances of that global variable
// may be generated (one for each translation unit) we add the name of the translation unit's source file.
// If that global variable definition is const, we add the name of the (maybe header) file that variable is defined in instead. This causes
// different instances of the variable that all MUST contain the same value to be merged into a single node in Sourcetrail.
std::string scopeFileName = "";
{
const clang::SourceManager& sourceManager = declaration->getASTContext().getSourceManager();
if (varDecl->getType().isConstQualified())
{
const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(declaration->getLocStart());
scopeFileName = FilePath(presumedBegin.getFilename()).fileName();
}
else
{
clang::FileID fileId = sourceManager.getMainFileID();
if (fileId.isValid())
{
const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId);
scopeFileName = FilePath(utility::getFileNameOfFileEntry(fileEntry)).fileName();
}
}
}
if (!scopeFileName.empty())
{
varName = declNameString + " (" + scopeFileName + ")";
}
}
return std::make_shared<CxxVariableDeclName>(varName, std::vector<std::string>(), typeName, isStatic);
}
}