Skip to content

Commit

Permalink
Merge pull request #77 from samanpa/optional_type
Browse files Browse the repository at this point in the history
Correctly find the location when a macro is called within a macro
  • Loading branch information
samanpa authored Jun 1, 2023
2 parents b0a6d6d + ed01745 commit 05eeb4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/collectors/include_graph/include_graph_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ static void add_usage(clang::CompilerInstance *ci, IncludeGraphData *data,
data->usage_reference_count[edge]++;
}

// Gets a sensible clang::SourceLocation even in the presence of a macro.
clang::SourceLocation get_canonical_location(clang::SourceManager &sm,
clang::SourceLocation loc) {
clang::SourceLocation spellingLoc = sm.getSpellingLoc(loc);
// this usually happens when there is token pasting
if (sm.isWrittenInScratchSpace(spellingLoc)) {
loc = sm.getExpansionRange(loc).getBegin();
}
return loc;
}

void add_macro_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
clangmetatool::types::MacroReferenceInfo m) {
if (!std::get<1>(m))
Expand All @@ -179,7 +190,8 @@ void add_macro_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
if (!info)
return;

clang::SourceLocation usageLoc = std::get<0>(m).getLocation();
clang::SourceLocation usageLoc = get_canonical_location(
ci->getSourceManager(), std::get<0>(m).getLocation());
clang::SourceLocation defLoc = info->getDefinitionLoc();

add_usage(ci, data, usageLoc, defLoc, m, data->macro_references);
Expand All @@ -196,17 +208,6 @@ void add_redeclaration(clang::CompilerInstance *ci, IncludeGraphData *data,
data->redeclarations);
}

// Gets a sensible clang::SourceLocation even in the presence of a macro.
clang::SourceLocation get_canonical_location(clang::SourceManager &sm,
clang::SourceLocation loc) {
clang::SourceLocation spellingLoc = sm.getSpellingLoc(loc);
// this usually happens when there is token pasting
if (sm.isWrittenInScratchSpace(spellingLoc)) {
loc = sm.getExpansionRange(loc).getBegin();
}
return loc;
}

void add_decl_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
const clang::DeclRefExpr *e) {

Expand Down
4 changes: 3 additions & 1 deletion t/data/044-type-access-through-expansion/foo.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "foo.h"
#include "macro.h"

#define CALL_FOO2(v) FOO2(v)

void function() {
FOO1() f1;
FOO2(v);
FOO1() f3;
CALL_FOO2(f3);
}

0 comments on commit 05eeb4d

Please sign in to comment.