Skip to content

Commit

Permalink
When we match a type usage try to retrieve the declaration of the typ…
Browse files Browse the repository at this point in the history
…e instead

of infering it

Signed-off-by: Kojo Adams <[email protected]>
  • Loading branch information
Kojo Adams authored and ruoso committed Jan 17, 2023
1 parent cb17e41 commit 6c24de8
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 16 deletions.
10 changes: 0 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ services:

matrix:
include:
- os: linux
compiler: gcc
language: cpp
sudo: true
script: docker build --build-arg TARGET_LLVM_VERSION=8 .
- os: linux
compiler: gcc
language: cpp
sudo: true
script: docker build --build-arg TARGET_LLVM_VERSION=9 .
- os: linux
compiler: gcc
language: cpp
Expand Down
4 changes: 2 additions & 2 deletions src/collectors/include_graph/find_type_match_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ void FindTypeMatchCallback::run(
const clang::ast_matchers::MatchFinder::MatchResult &r) {

if (const clang::TypeLoc *tl = r.Nodes.getNodeAs<clang::TypeLoc>("type")) {

add_type_reference(ci, data, tl);
const clang::Decl* decl = r.Nodes.getNodeAs<clang::Decl>("decl");
add_type_reference(ci, data, tl, decl);
}
}
} // namespace include_graph
Expand Down
3 changes: 2 additions & 1 deletion src/collectors/include_graph/include_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
namespace clangmetatool {
namespace collectors {

using namespace clang::ast_matchers;
class IncludeGraphImpl {
private:
clang::CompilerInstance *ci;
Expand All @@ -56,7 +57,7 @@ class IncludeGraphImpl {
clang::ast_matchers::StatementMatcher sm1 =
clang::ast_matchers::declRefExpr().bind("ref");
clang::ast_matchers::TypeLocMatcher sm2 =
clang::ast_matchers::typeLoc().bind("type");
clang::ast_matchers::typeLoc(optionally(loc((const internal::Matcher<clang::QualType> &)hasDeclaration(decl().bind("decl"))))).bind("type");
clang::ast_matchers::DeclarationMatcher sm3 =
clang::ast_matchers::decl().bind("decl");

Expand Down
3 changes: 1 addition & 2 deletions src/collectors/include_graph/include_graph_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ bool check_for_first_end(clang::CompilerInstance *ci, IncludeGraphData *data,
}

void add_type_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
const clang::TypeLoc *n) {
const clang::TypeLoc *n, const clang::Decl* decl) {

const clang::Type *t = n->getTypePtr();
const clang::Decl *decl = NULL;
if (!decl)
decl = extract_decl_for_type<clang::TypedefType>(t);
if (!decl)
Expand Down
2 changes: 1 addition & 1 deletion src/collectors/include_graph/include_graph_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void add_decl_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
const clang::DeclRefExpr *n);

void add_type_reference(clang::CompilerInstance *ci, IncludeGraphData *data,
const clang::TypeLoc *n);
const clang::TypeLoc *n, const clang::Decl* decl);
} // namespace include_graph
} // namespace collectors
} // namespace clangmetatool
Expand Down
86 changes: 86 additions & 0 deletions t/042-generic-using-include-graph.t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "clangmetatool-testconfig.h"


#include <gtest/gtest.h>
#include <map>
#include <string>
#include <iostream>

#include <clangmetatool/collectors/include_graph.h>
#include <clangmetatool/collectors/include_graph_data.h>
#include <clangmetatool/include_graph_dependencies.h>
#include <clangmetatool/meta_tool.h>
#include <clangmetatool/meta_tool_factory.h>

#include <clang/Frontend/FrontendAction.h>
#include <clang/Tooling/CommonOptionsParser.h>
#include <clang/Tooling/Core/Replacement.h>
#include <clang/Tooling/Refactoring.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/Support/CommandLine.h>

class WeakDependenciesTool {
private:
clang::CompilerInstance *ci;
clangmetatool::collectors::IncludeGraph includeGraph;

public:
WeakDependenciesTool(clang::CompilerInstance *ci,
clang::ast_matchers::MatchFinder *f)
: ci(ci), includeGraph(ci, f) {}
void postProcessing(
std::map<std::string, clang::tooling::Replacements> &replacementMap) {
clangmetatool::collectors::IncludeGraphData *data = includeGraph.getData();

std::map<std::string, clangmetatool::types::FileUID> fname2uid;
for (auto itr = data->fuid2name.begin(); itr != data->fuid2name.end();
++itr) {
fname2uid[itr->second] = itr->first;
}

// Get main file UID
clang::SourceManager &sm = ci->getSourceManager();
const clang::FileEntry *mfe = sm.getFileEntryForID(sm.getMainFileID());
clangmetatool::types::FileUID mfid = mfe->getUID();
EXPECT_EQ(
clangmetatool::IncludeGraphDependencies::DirectDependenciesMap(
{std::make_pair(
fname2uid["generic_using.h"],
std::set<clangmetatool::types::FileUID>{fname2uid["generic_using.h"]})}),
clangmetatool::IncludeGraphDependencies::liveWeakDependencies(data,
mfid));

clangmetatool::IncludeGraphDependencies::decrementUsageRefCount(
data, {mfid, fname2uid["generic_using.h"]});

// generic_using.h no longer needed
EXPECT_EQ(
clangmetatool::IncludeGraphDependencies::DirectDependenciesMap({}),
clangmetatool::IncludeGraphDependencies::liveWeakDependencies(data,
mfid));

}
};

TEST(include_validate_test, liveWeakDependencies) {
llvm::cl::OptionCategory MyToolCategory("my-tool options");

int argc = 4;
const char *argv[] = {
"foo", CMAKE_SOURCE_DIR "/t/data/042-generic-using-include-graph/foo.cpp",
"--", "-xc++"};

auto result = clang::tooling::CommonOptionsParser::create(
argc, argv, MyToolCategory, llvm::cl::OneOrMore);
ASSERT_TRUE(!!result);
clang::tooling::CommonOptionsParser &optionsParser = result.get();

clang::tooling::RefactoringTool tool(optionsParser.getCompilations(),
optionsParser.getSourcePathList());

clangmetatool::MetaToolFactory<clangmetatool::MetaTool<WeakDependenciesTool>>
raf(tool.getReplacements());

int r = tool.runAndSave(&raf);
ASSERT_EQ(0, r);
}
1 change: 1 addition & 0 deletions t/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ foreach(
039-includegraph-nested-name-parameter
040-includegraph-nested-using
041-expand-range-if-valid
042-generic-using-include-graph
)

add_executable(${TEST}.t ${TEST}.t.cpp)
Expand Down
5 changes: 5 additions & 0 deletions t/data/042-generic-using-include-graph/foo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "generic_using.h"

void func() {
generic_using<int> td;
}
6 changes: 6 additions & 0 deletions t/data/042-generic-using-include-graph/generic_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

template <typename T>
struct generic_type {
T val;
};
6 changes: 6 additions & 0 deletions t/data/042-generic-using-include-graph/generic_using.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

#include "generic_type.h"

template <typename T>
using generic_using = generic_type<T>;

0 comments on commit 6c24de8

Please sign in to comment.