diff --git a/.travis.yml b/.travis.yml index 778f0f5..40e5b89 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/collectors/include_graph/find_type_match_callback.cpp b/src/collectors/include_graph/find_type_match_callback.cpp index 218e628..29350bc 100644 --- a/src/collectors/include_graph/find_type_match_callback.cpp +++ b/src/collectors/include_graph/find_type_match_callback.cpp @@ -49,8 +49,8 @@ void FindTypeMatchCallback::run( const clang::ast_matchers::MatchFinder::MatchResult &r) { if (const clang::TypeLoc *tl = r.Nodes.getNodeAs("type")) { - - add_type_reference(ci, data, tl); + const clang::Decl* decl = r.Nodes.getNodeAs("decl"); + add_type_reference(ci, data, tl, decl); } } } // namespace include_graph diff --git a/src/collectors/include_graph/include_graph.cpp b/src/collectors/include_graph/include_graph.cpp index 020cb43..2d7053e 100644 --- a/src/collectors/include_graph/include_graph.cpp +++ b/src/collectors/include_graph/include_graph.cpp @@ -48,6 +48,7 @@ namespace clangmetatool { namespace collectors { +using namespace clang::ast_matchers; class IncludeGraphImpl { private: clang::CompilerInstance *ci; @@ -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 &)hasDeclaration(decl().bind("decl"))))).bind("type"); clang::ast_matchers::DeclarationMatcher sm3 = clang::ast_matchers::decl().bind("decl"); diff --git a/src/collectors/include_graph/include_graph_util.cpp b/src/collectors/include_graph/include_graph_util.cpp index 7bd08c2..83d5ad2 100644 --- a/src/collectors/include_graph/include_graph_util.cpp +++ b/src/collectors/include_graph/include_graph_util.cpp @@ -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(t); if (!decl) diff --git a/src/collectors/include_graph/include_graph_util.h b/src/collectors/include_graph/include_graph_util.h index 6a88e8d..0bb3b64 100644 --- a/src/collectors/include_graph/include_graph_util.h +++ b/src/collectors/include_graph/include_graph_util.h @@ -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 diff --git a/t/042-generic-using-include-graph.t.cpp b/t/042-generic-using-include-graph.t.cpp new file mode 100644 index 0000000..d667b94 --- /dev/null +++ b/t/042-generic-using-include-graph.t.cpp @@ -0,0 +1,86 @@ +#include "clangmetatool-testconfig.h" + + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +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 &replacementMap) { + clangmetatool::collectors::IncludeGraphData *data = includeGraph.getData(); + + std::map 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{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> + raf(tool.getReplacements()); + + int r = tool.runAndSave(&raf); + ASSERT_EQ(0, r); +} diff --git a/t/CMakeLists.txt b/t/CMakeLists.txt index 8ae721a..6e11e25 100644 --- a/t/CMakeLists.txt +++ b/t/CMakeLists.txt @@ -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) diff --git a/t/data/042-generic-using-include-graph/foo.cpp b/t/data/042-generic-using-include-graph/foo.cpp new file mode 100644 index 0000000..d945842 --- /dev/null +++ b/t/data/042-generic-using-include-graph/foo.cpp @@ -0,0 +1,5 @@ +#include "generic_using.h" + +void func() { + generic_using td; +} diff --git a/t/data/042-generic-using-include-graph/generic_type.h b/t/data/042-generic-using-include-graph/generic_type.h new file mode 100644 index 0000000..15d2dd0 --- /dev/null +++ b/t/data/042-generic-using-include-graph/generic_type.h @@ -0,0 +1,6 @@ +#pragma once + +template +struct generic_type { + T val; +}; diff --git a/t/data/042-generic-using-include-graph/generic_using.h b/t/data/042-generic-using-include-graph/generic_using.h new file mode 100644 index 0000000..d7d1981 --- /dev/null +++ b/t/data/042-generic-using-include-graph/generic_using.h @@ -0,0 +1,6 @@ +#pragma once + +#include "generic_type.h" + +template +using generic_using = generic_type;