Index: include-fixer/find-all-symbols/CMakeLists.txt =================================================================== --- include-fixer/find-all-symbols/CMakeLists.txt +++ include-fixer/find-all-symbols/CMakeLists.txt @@ -4,7 +4,9 @@ add_clang_library(findAllSymbols FindAllSymbols.cpp + FindAllSymbolsAction.cpp FindAllMacros.cpp + HeaderMapCollector.cpp PragmaCommentHandler.cpp SymbolInfo.cpp Index: include-fixer/find-all-symbols/FindAllMacros.cpp =================================================================== --- include-fixer/find-all-symbols/FindAllMacros.cpp +++ include-fixer/find-all-symbols/FindAllMacros.cpp @@ -28,11 +28,7 @@ return; // If Collector is not nullptr, check pragma remapping header. - if (Collector) { - auto Iter = Collector->getHeaderMappingTable().find(FilePath); - if (Iter != Collector->getHeaderMappingTable().end()) - FilePath = Iter->second; - } + FilePath = Collector ? Collector->getMappedHeader(FilePath) : FilePath; SymbolInfo Symbol(MacroNameTok.getIdentifierInfo()->getName(), SymbolInfo::SymbolKind::Macro, FilePath.str(), Index: include-fixer/find-all-symbols/FindAllSymbols.cpp =================================================================== --- include-fixer/find-all-symbols/FindAllSymbols.cpp +++ include-fixer/find-all-symbols/FindAllSymbols.cpp @@ -97,11 +97,7 @@ return llvm::None; // If Collector is not nullptr, check pragma remapping header. - if (Collector) { - auto Iter = Collector->getHeaderMappingTable().find(FilePath); - if (Iter != Collector->getHeaderMappingTable().end()) - FilePath = Iter->second; - } + FilePath = Collector ? Collector->getMappedHeader(FilePath) : FilePath; return SymbolInfo(ND->getNameAsString(), Type, FilePath.str(), SM.getExpansionLineNumber(Loc), GetContexts(ND)); Index: include-fixer/find-all-symbols/FindAllSymbolsAction.h =================================================================== --- /dev/null +++ include-fixer/find-all-symbols/FindAllSymbolsAction.h @@ -0,0 +1,61 @@ +//===-- FindAllSymbols.h - find all symbols----------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_FIND_ALL_SYMBOLS_ACTION_H +#define LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_FIND_ALL_SYMBOLS_ACTION_H + +#include "FindAllMacros.h" +#include "FindAllSymbols.h" +#include "HeaderMapCollector.h" +#include "PragmaCommentHandler.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/FrontendActions.h" +#include "clang/Tooling/Tooling.h" + +namespace clang { +namespace find_all_symbols { + +class FindAllSymbolsAction : public clang::ASTFrontendAction { +public: + explicit FindAllSymbolsAction( + SymbolReporter *Reporter, + const HeaderMapCollector::HeaderMap *PostfixMap = nullptr); + + std::unique_ptr + CreateASTConsumer(clang::CompilerInstance &Compiler, + StringRef InFile) override; + +private: + SymbolReporter *const Reporter; + clang::ast_matchers::MatchFinder MatchFinder; + HeaderMapCollector Collector; + PragmaCommentHandler Handler; + FindAllSymbols Matcher; +}; + +class FindAllSymbolsActionFactory : public tooling::FrontendActionFactory { +public: + FindAllSymbolsActionFactory( + SymbolReporter *Reporter, + const HeaderMapCollector::HeaderMap *PostfixMap = nullptr) + : Reporter(Reporter), PostfixMap(PostfixMap) {} + + virtual clang::FrontendAction *create() override { + return new FindAllSymbolsAction(Reporter, PostfixMap); + } + +private: + SymbolReporter *const Reporter; + const HeaderMapCollector::HeaderMap *const PostfixMap; +}; + +} // namespace find_all_symbols +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_FIND_ALL_SYMBOLS_ACTION_H Index: include-fixer/find-all-symbols/FindAllSymbolsAction.cpp =================================================================== --- /dev/null +++ include-fixer/find-all-symbols/FindAllSymbolsAction.cpp @@ -0,0 +1,32 @@ +//===-- FindAllSymbolsMain.cpp - find all symbols action --------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "FindAllSymbolsAction.h" + +namespace clang { +namespace find_all_symbols { + +FindAllSymbolsAction::FindAllSymbolsAction( + SymbolReporter *Reporter, const HeaderMapCollector::HeaderMap *PostfixMap) + : Reporter(Reporter), Collector(PostfixMap), Handler(&Collector), + Matcher(Reporter, &Collector) { + Matcher.registerMatchers(&MatchFinder); +} + +std::unique_ptr +FindAllSymbolsAction::CreateASTConsumer(clang::CompilerInstance &Compiler, + StringRef InFile) { + Compiler.getPreprocessor().addCommentHandler(&Handler); + Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique( + Reporter, &Compiler.getSourceManager(), &Collector)); + return MatchFinder.newASTConsumer(); +} + +} // namespace find_all_symbols +} // namespace clang Index: include-fixer/find-all-symbols/HeaderMapCollector.h =================================================================== --- include-fixer/find-all-symbols/HeaderMapCollector.h +++ include-fixer/find-all-symbols/HeaderMapCollector.h @@ -16,20 +16,36 @@ namespace clang { namespace find_all_symbols { -/// \brief HeaderMappCollector collects all remapping header files. +/// \brief HeaderMappCollector collects all remapping header files. This maps +/// complete header names or postfixes of header names to header names. class HeaderMapCollector { public: typedef llvm::StringMap HeaderMap; + HeaderMapCollector() : PostfixMappingTable(nullptr) {} + + explicit HeaderMapCollector(const HeaderMap *PostfixMap) + : PostfixMappingTable(PostfixMap) {} + void addHeaderMapping(llvm::StringRef OrignalHeaderPath, llvm::StringRef MappingHeaderPath) { HeaderMappingTable[OrignalHeaderPath] = MappingHeaderPath; }; - const HeaderMap &getHeaderMappingTable() const { return HeaderMappingTable; }; + + /// Check if there is a mapping from \p Header or its postfix to another + /// header name. + /// \param Header A header name. + /// \return \p Header itself if there is no mapping for it; otherwise, return + /// a mapped header name. + llvm::StringRef getMappedHeader(llvm::StringRef Header) const; private: /// A string-to-string map saving the mapping relationship. HeaderMap HeaderMappingTable; + + // A postfix-to-header name map. + // This is a reference to a hard-coded map. + const HeaderMap *const PostfixMappingTable; }; } // namespace find_all_symbols Index: include-fixer/find-all-symbols/HeaderMapCollector.cpp =================================================================== --- /dev/null +++ include-fixer/find-all-symbols/HeaderMapCollector.cpp @@ -0,0 +1,34 @@ +//===-- HeaderMapCoolector.h - find all symbols------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "HeaderMapCollector.h" + +namespace clang { +namespace find_all_symbols { + +llvm::StringRef +HeaderMapCollector::getMappedHeader(llvm::StringRef Header) const { + auto Iter = HeaderMappingTable.find(Header); + if (Iter != HeaderMappingTable.end()) + return Iter->second; + // If there is no complete header name mapping for this header, check the + // postfix mapping. + // FIXME: this is not very efficient. Change PostfixMappingTable to use + // postfix tree if necessary. + if (PostfixMappingTable) { + for (const auto &Entry : *PostfixMappingTable) { + if (Header.endswith(Entry.first())) + return Entry.second; + } + } + return Header; +} + +} // namespace find_all_symbols +} // namespace clang Index: include-fixer/find-all-symbols/tool/CMakeLists.txt =================================================================== --- include-fixer/find-all-symbols/tool/CMakeLists.txt +++ include-fixer/find-all-symbols/tool/CMakeLists.txt @@ -1,6 +1,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) -add_clang_executable(find-all-symbols FindAllSymbolsMain.cpp) +add_clang_executable(find-all-symbols + FindAllSymbolsMain.cpp + STLPostfixHeaderMap.cpp + ) target_link_libraries(find-all-symbols clangAST Index: include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp =================================================================== --- include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp +++ include-fixer/find-all-symbols/tool/FindAllSymbolsMain.cpp @@ -7,10 +7,8 @@ // //===----------------------------------------------------------------------===// -#include "FindAllMacros.h" -#include "FindAllSymbols.h" -#include "HeaderMapCollector.h" -#include "PragmaCommentHandler.h" +#include "FindAllSymbolsAction.h" +#include "STLPostfixHeaderMap.h" #include "SymbolInfo.h" #include "SymbolReporter.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -61,60 +59,32 @@ The directory for merging symbols.)"), cl::init(""), cl::cat(FindAllSymbolsCategory)); - namespace clang { namespace find_all_symbols { class YamlReporter : public clang::find_all_symbols::SymbolReporter { public: - ~YamlReporter() override {} - - void reportSymbol(StringRef FileName, const SymbolInfo &Symbol) override { - Symbols[FileName].insert(Symbol); - } - - void Write(const std::string &Dir) { + ~YamlReporter() override { for (const auto &Symbol : Symbols) { int FD; SmallString<128> ResultPath; llvm::sys::fs::createUniqueFile( - Dir + "/" + llvm::sys::path::filename(Symbol.first) + "-%%%%%%.yaml", + OutputDir + "/" + llvm::sys::path::filename(Symbol.first) + + "-%%%%%%.yaml", FD, ResultPath); llvm::raw_fd_ostream OS(FD, /*shouldClose=*/true); WriteSymbolInfosToStream(OS, Symbol.second); } } -private: - std::map> Symbols; -}; - -// FIXME: Move this out from the main file, make it reusable in unittest. -class FindAllSymbolsAction : public clang::ASTFrontendAction { -public: - FindAllSymbolsAction() - : Reporter(), MatchFinder(), Collector(), Handler(&Collector), - Matcher(&Reporter, &Collector) { - Matcher.registerMatchers(&MatchFinder); - } - - std::unique_ptr - CreateASTConsumer(clang::CompilerInstance &Compiler, - StringRef InFile) override { - Compiler.getPreprocessor().addCommentHandler(&Handler); - Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique( - &Reporter, &Compiler.getSourceManager(), &Collector)); - return MatchFinder.newASTConsumer(); + void reportSymbol(StringRef FileName, const SymbolInfo &Symbol) override { + Symbols[FileName].insert(Symbol); } - void EndSourceFileAction() override { Reporter.Write(OutputDir); } - private: - YamlReporter Reporter; - clang::ast_matchers::MatchFinder MatchFinder; - HeaderMapCollector Collector; - PragmaCommentHandler Handler; - FindAllSymbols Matcher; + // Directory to write yaml files to. + const std::string Directory; + std::map> Symbols; }; bool Merge(llvm::StringRef MergeDir, llvm::StringRef OutputFile) { @@ -176,8 +146,12 @@ clang::find_all_symbols::Merge(MergeDir, sources[0]); return 0; } - Tool.run( - newFrontendActionFactory() - .get()); + + clang::find_all_symbols::YamlReporter Reporter; + + auto Factory = + llvm::make_unique( + &Reporter, &clang::find_all_symbols::STLPostfixHeaderMap); + Tool.run(Factory.get()); return 0; } Index: include-fixer/find-all-symbols/tool/STLPostfixHeaderMap.h =================================================================== --- /dev/null +++ include-fixer/find-all-symbols/tool/STLPostfixHeaderMap.h @@ -0,0 +1,23 @@ +//===-- HeaderMapCoolector.h - hardcoded postfix header map -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_TOOL_HEADER_MAP_COLLECTOR_H +#define LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_TOOL_HEADER_MAP_COLLECTOR_H + +#include + +namespace clang { +namespace find_all_symbols { + +extern const HeaderMapCollector::HeaderMap STLPostfixHeaderMap; + +} // namespace find_all_symbols +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_FIND_ALL_SYMBOLS_TOOL_HEADER_MAP_COLLECTOR_H Index: include-fixer/find-all-symbols/tool/STLPostfixHeaderMap.cpp =================================================================== --- /dev/null +++ include-fixer/find-all-symbols/tool/STLPostfixHeaderMap.cpp @@ -0,0 +1,359 @@ + +//===-- HeaderMapCoolector.h - hardcoded postfix header ma ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "STLPostfixHeaderMap.h" + +namespace clang { +namespace find_all_symbols { + +const HeaderMapCollector::HeaderMap STLPostfixHeaderMap = { + {"include/__stddef_max_align_t.h", ""}, + {"include/__wmmintrin_aes.h", ""}, + {"include/__wmmintrin_pclmul.h", ""}, + {"include/adxintrin.h", ""}, + {"include/ammintrin.h", ""}, + {"include/avx2intrin.h", ""}, + {"include/avx512bwintrin.h", ""}, + {"include/avx512cdintrin.h", ""}, + {"include/avx512dqintrin.h", ""}, + {"include/avx512erintrin.h", ""}, + {"include/avx512fintrin.h", ""}, + {"include/avx512ifmaintrin.h", ""}, + {"include/avx512ifmavlintrin.h", ""}, + {"include/avx512pfintrin.h", ""}, + {"include/avx512vbmiintrin.h", ""}, + {"include/avx512vbmivlintrin.h", ""}, + {"include/avx512vlbwintrin.h", ""}, + {"include/avx512vlcdintrin.h", ""}, + {"include/avx512vldqintrin.h", ""}, + {"include/avx512vlintrin.h", ""}, + {"include/avxintrin.h", ""}, + {"include/bmi2intrin.h", ""}, + {"include/bmiintrin.h", ""}, + {"include/emmintrin.h", ""}, + {"include/f16cintrin.h", ""}, + {"include/float.h", ""}, + {"include/fma4intrin.h", ""}, + {"include/fmaintrin.h", ""}, + {"include/fxsrintrin.h", ""}, + {"include/ia32intrin.h", ""}, + {"include/immintrin.h", ""}, + {"include/inttypes.h", ""}, + {"include/limits.h", ""}, + {"include/lzcntintrin.h", ""}, + {"include/mm3dnow.h", ""}, + {"include/mm_malloc.h", ""}, + {"include/mmintrin.h", ""}, + {"include/mwaitxintrin.h", ""}, + {"include/pkuintrin.h", ""}, + {"include/pmmintrin.h", ""}, + {"include/popcntintrin.h", ""}, + {"include/prfchwintrin.h", ""}, + {"include/rdseedintrin.h", ""}, + {"include/rtmintrin.h", ""}, + {"include/shaintrin.h", ""}, + {"include/smmintrin.h", ""}, + {"include/stdalign.h", ""}, + {"include/stdarg.h", ""}, + {"include/stdbool.h", ""}, + {"include/stddef.h", ""}, + {"include/stdint.h", ""}, + {"include/tbmintrin.h", ""}, + {"include/tmmintrin.h", ""}, + {"include/wmmintrin.h", ""}, + {"include/x86intrin.h", ""}, + {"include/xmmintrin.h", ""}, + {"include/xopintrin.h", ""}, + {"include/xsavecintrin.h", ""}, + {"include/xsaveintrin.h", ""}, + {"include/xsaveoptintrin.h", ""}, + {"include/xsavesintrin.h", ""}, + {"include/xtestintrin.h", ""}, + {"include/_G_config.h", ""}, + {"include/alloca.h", ""}, + {"include/asm-generic/errno-base.h", ""}, + {"include/asm-generic/errno.h", ""}, + {"/usr/include/assert.h", ""}, + {"algorithm", ""}, + {"array", ""}, + {"atomic", ""}, + {"backward/auto_ptr.h", ""}, + {"backward/binders.h", ""}, + {"bits/algorithmfwd.h", ""}, + {"bits/alloc_traits.h", ""}, + {"bits/allocator.h", ""}, + {"bits/atomic_base.h", ""}, + {"bits/atomic_lockfree_defines.h", ""}, + {"bits/basic_ios.h", ""}, + {"bits/basic_ios.tcc", ""}, + {"bits/basic_string.h", ""}, + {"bits/basic_string.tcc", ""}, + {"bits/char_traits.h", ""}, + {"bits/codecvt.h", ""}, + {"bits/concept_check.h", ""}, + {"bits/cpp_type_traits.h", ""}, + {"bits/cxxabi_forced.h", ""}, + {"bits/deque.tcc", ""}, + {"bits/exception_defines.h", ""}, + {"bits/exception_ptr.h", ""}, + {"bits/forward_list.h", ""}, + {"bits/forward_list.tcc", ""}, + {"bits/fstream.tcc", ""}, + {"bits/functexcept.h", ""}, + {"bits/functional_hash.h", ""}, + {"bits/gslice.h", ""}, + {"bits/gslice_array.h", ""}, + {"bits/hash_bytes.h", ""}, + {"bits/hashtable.h", ""}, + {"bits/hashtable_policy.h", ""}, + {"bits/indirect_array.h", ""}, + {"bits/ios_base.h", ""}, + {"bits/istream.tcc", ""}, + {"bits/list.tcc", ""}, + {"bits/locale_classes.h", ""}, + {"bits/locale_classes.tcc", ""}, + {"bits/locale_facets.h", ""}, + {"bits/locale_facets.tcc", ""}, + {"bits/locale_facets_nonio.h", ""}, + {"bits/locale_facets_nonio.tcc", ""}, + {"bits/localefwd.h", ""}, + {"bits/mask_array.h", ""}, + {"bits/memoryfwd.h", ""}, + {"bits/move.h", ""}, + {"bits/nested_exception.h", ""}, + {"bits/ostream.tcc", ""}, + {"bits/ostream_insert.h", ""}, + {"bits/postypes.h", ""}, + {"bits/ptr_traits.h", ""}, + {"bits/random.h", ""}, + {"bits/random.tcc", ""}, + {"bits/range_access.h", ""}, + {"bits/regex.h", ""}, + {"bits/regex_compiler.h", ""}, + {"bits/regex_constants.h", ""}, + {"bits/regex_cursor.h", ""}, + {"bits/regex_error.h", ""}, + {"bits/regex_grep_matcher.h", ""}, + {"bits/regex_grep_matcher.tcc", ""}, + {"bits/regex_nfa.h", ""}, + {"bits/shared_ptr.h", ""}, + {"bits/shared_ptr_base.h", ""}, + {"bits/slice_array.h", ""}, + {"bits/sstream.tcc", ""}, + {"bits/stl_algo.h", ""}, + {"bits/stl_algobase.h", ""}, + {"bits/stl_bvector.h", ""}, + {"bits/stl_construct.h", ""}, + {"bits/stl_deque.h", ""}, + {"bits/stl_function.h", ""}, + {"bits/stl_heap.h", ""}, + {"bits/stl_iterator.h", ""}, + {"bits/stl_iterator_base_funcs.h", ""}, + {"bits/stl_iterator_base_types.h", ""}, + {"bits/stl_list.h", ""}, + {"bits/stl_map.h", ""}, + {"bits/stl_multimap.h", ""}, + {"bits/stl_multiset.h", ""}, + {"bits/stl_numeric.h", ""}, + {"bits/stl_pair.h", ""}, + {"bits/stl_queue.h", ""}, + {"bits/stl_raw_storage_iter.h", ""}, + {"bits/stl_relops.h", ""}, + {"bits/stl_set.h", ""}, + {"bits/stl_stack.h", ""}, + {"bits/stl_tempbuf.h", ""}, + {"bits/stl_tree.h", ""}, + {"bits/stl_uninitialized.h", ""}, + {"bits/stl_vector.h", ""}, + {"bits/stream_iterator.h", ""}, + {"bits/streambuf.tcc", ""}, + {"bits/streambuf_iterator.h", ""}, + {"bits/stringfwd.h", ""}, + {"bits/unique_ptr.h", ""}, + {"bits/unordered_map.h", ""}, + {"bits/unordered_set.h", ""}, + {"bits/uses_allocator.h", ""}, + {"bits/valarray_after.h", ""}, + {"bits/valarray_array.h", ""}, + {"bits/valarray_array.tcc", ""}, + {"bits/valarray_before.h", ""}, + {"bits/vector.tcc", ""}, + {"bitset", ""}, + {"ccomplex", ""}, + {"cctype", ""}, + {"cerrno", ""}, + {"cfenv", ""}, + {"cfloat", ""}, + {"chrono", ""}, + {"cinttypes", ""}, + {"climits", ""}, + {"clocale", ""}, + {"cmath", ""}, + {"complex", ""}, + {"complex.h", ""}, + {"condition_variable", ""}, + {"csetjmp", ""}, + {"csignal", ""}, + {"cstdalign", ""}, + {"cstdarg", ""}, + {"cstdbool", ""}, + {"cstdint", ""}, + {"cstdio", ""}, + {"cstdlib", ""}, + {"cstring", ""}, + {"ctgmath", ""}, + {"ctime", ""}, + {"cwchar", ""}, + {"cwctype", ""}, + {"cxxabi.h", ""}, + {"debug/debug.h", ""}, + {"deque", ""}, + {"exception", ""}, + {"ext/alloc_traits.h", ""}, + {"ext/atomicity.h", ""}, + {"ext/concurrence.h", ""}, + {"ext/new_allocator.h", ""}, + {"ext/numeric_traits.h", ""}, + {"ext/string_conversions.h", ""}, + {"ext/type_traits.h", ""}, + {"fenv.h", ""}, + {"forward_list", ""}, + {"fstream", ""}, + {"functional", ""}, + {"future", ""}, + {"initializer_list", ""}, + {"iomanip", ""}, + {"ios", ""}, + {"iosfwd", ""}, + {"iostream", ""}, + {"istream", ""}, + {"iterator", ""}, + {"limits", ""}, + {"list", ""}, + {"locale", ""}, + {"map", ""}, + {"memory", ""}, + {"mutex", ""}, + {"new", ""}, + {"numeric", ""}, + {"ostream", ""}, + {"queue", ""}, + {"random", ""}, + {"ratio", ""}, + {"regex", ""}, + {"scoped_allocator", ""}, + {"set", ""}, + {"sstream", ""}, + {"stack", ""}, + {"stdexcept", ""}, + {"streambuf", ""}, + {"string", ""}, + {"system_error", ""}, + {"tgmath.h", ""}, + {"thread", ""}, + {"tuple", ""}, + {"type_traits", ""}, + {"typeindex", ""}, + {"typeinfo", ""}, + {"unordered_map", ""}, + {"unordered_set", ""}, + {"utility", ""}, + {"valarray", ""}, + {"vector", ""}, + {"include/complex.h", ""}, + {"include/ctype.h", ""}, + {"include/endian.h", ""}, + {"include/errno.h", ""}, + {"include/features.h", ""}, + {"include/fenv.h", ""}, + {"include/inttypes.h", ""}, + {"include/libintl.h", ""}, + {"include/libio.h", ""}, + {"include/limits.h", ""}, + {"include/linux/limits.h", ""}, + {"include/locale.h", ""}, + {"include/math.h", ""}, + {"include/pthread.h", ""}, + {"include/sched.h", ""}, + {"include/setjmp.h", ""}, + {"include/signal.h", ""}, + {"include/stdc-predef.h", ""}, + {"include/stdint.h", ""}, + {"include/stdio.h", ""}, + {"include/stdlib.h", ""}, + {"include/string.h", ""}, + {"include/time.h", ""}, + {"include/wchar.h", ""}, + {"include/wctype.h", ""}, + {"bits/byteswap-16.h", ""}, + {"bits/byteswap.h", ""}, + {"bits/cmathcalls.h", ""}, + {"bits/endian.h", ""}, + {"bits/errno.h", ""}, + {"bits/fenv.h", ""}, + {"bits/huge_val.h", ""}, + {"bits/huge_valf.h", ""}, + {"bits/huge_vall.h", ""}, + {"bits/inf.h", ""}, + {"bits/local_lim.h", ""}, + {"bits/locale.h", ""}, + {"bits/mathcalls.h", ""}, + {"bits/mathdef.h", ""}, + {"bits/nan.h", ""}, + {"bits/posix1_lim.h", ""}, + {"bits/posix2_lim.h", ""}, + {"bits/pthreadtypes.h", ""}, + {"bits/sched.h", ""}, + {"bits/select.h", ""}, + {"bits/setjmp.h", ""}, + {"bits/sigaction.h", ""}, + {"bits/sigcontext.h", ""}, + {"bits/siginfo.h", ""}, + {"bits/signum.h", ""}, + {"bits/sigset.h", ""}, + {"bits/sigstack.h", ""}, + {"bits/sigthread.h", ""}, + {"bits/stdio_lim.h", ""}, + {"bits/sys_errlist.h", ""}, + {"bits/time.h", ""}, + {"bits/timex.h", ""}, + {"bits/types.h", ""}, + {"bits/typesizes.h", ""}, + {"bits/waitflags.h", ""}, + {"bits/waitstatus.h", ""}, + {"bits/wchar.h", ""}, + {"bits/wordsize.h", ""}, + {"bits/xopen_lim.h", ""}, + {"gnu/stubs-64.h", ""}, + {"sys/cdefs.h", ""}, + {"sys/select.h", ""}, + {"sys/sysmacros.h", ""}, + {"sys/types.h", ""}, + {"sys/ucontext.h", ""}, + {"/usr/include/xlocale.h", ""}, + {"bits/atomic_word.h", ""}, + {"bits/basic_file.h", ""}, + {"bits/c++allocator.h", ""}, + {"bits/c++config.h", ""}, + {"bits/c++io.h", ""}, + {"bits/c++locale.h", ""}, + {"bits/cpu_defines.h", ""}, + {"bits/ctype_base.h", ""}, + {"bits/cxxabi_tweaks.h", ""}, + {"bits/error_constants.h", ""}, + {"bits/gthr-default.h", ""}, + {"bits/gthr.h", ""}, + {"bits/opt_random.h", ""}, + {"bits/os_defines.h", ""}, +}; + +} // namespace find_all_symbols +} // namespace find_all_symbols + Index: unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp =================================================================== --- unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp +++ unittests/include-fixer/find-all-symbols/FindAllSymbolsTests.cpp @@ -7,10 +7,8 @@ // //===----------------------------------------------------------------------===// -#include "FindAllMacros.h" -#include "FindAllSymbols.h" +#include "FindAllSymbolsAction.h" #include "HeaderMapCollector.h" -#include "PragmaCommentHandler.h" #include "SymbolInfo.h" #include "SymbolReporter.h" #include "clang/ASTMatchers/ASTMatchFinder.h" @@ -54,44 +52,6 @@ std::vector Symbols; }; -class TestFindAllSymbolsAction : public clang::ASTFrontendAction { -public: - TestFindAllSymbolsAction(SymbolReporter *Reporter) - : Reporter(Reporter), MatchFinder(), Collector(), Handler(&Collector), - Matcher(Reporter, &Collector) { - Matcher.registerMatchers(&MatchFinder); - } - - std::unique_ptr - CreateASTConsumer(clang::CompilerInstance &Compiler, - StringRef InFile) override { - Compiler.getPreprocessor().addCommentHandler(&Handler); - Compiler.getPreprocessor().addPPCallbacks(llvm::make_unique( - Reporter, &Compiler.getSourceManager(), &Collector)); - return MatchFinder.newASTConsumer(); - } - -private: - SymbolReporter *const Reporter; - ast_matchers::MatchFinder MatchFinder; - HeaderMapCollector Collector; - PragmaCommentHandler Handler; - FindAllSymbols Matcher; -}; - -class TestFindAllSymbolsActionFactory - : public clang::tooling::FrontendActionFactory { -public: - TestFindAllSymbolsActionFactory(TestSymbolReporter *Reporter) - : Reporter(Reporter) {} - clang::FrontendAction *create() override { - return new TestFindAllSymbolsAction(Reporter); - } - -private: - TestSymbolReporter *const Reporter; -}; - class FindAllSymbolsTest : public ::testing::Test { public: bool hasSymbol(const SymbolInfo &Symbol) { @@ -106,8 +66,20 @@ std::string FileName = "symbol.cc"; + const std::string InternalHeader = "internal/internal.h"; + const std::string TopHeader = ""; + HeaderMapCollector::HeaderMap PostfixMap = { + {InternalHeader, TopHeader}, + }; + + std::string InternalCode = "class Internal {};"; + SymbolInfo InternalSymbol("Internal", SymbolInfo::SymbolKind::Class, + TopHeader, 1, {}); + InMemoryFileSystem->addFile(InternalHeader, 0, + llvm::MemoryBuffer::getMemBuffer(InternalCode)); + std::unique_ptr Factory( - new TestFindAllSymbolsActionFactory(&Reporter)); + new FindAllSymbolsActionFactory(&Reporter, &PostfixMap)); tooling::ToolInvocation Invocation( {std::string("find_all_symbols"), std::string("-fsyntax-only"), @@ -118,10 +90,13 @@ InMemoryFileSystem->addFile(HeaderName, 0, llvm::MemoryBuffer::getMemBuffer(Code)); - std::string Content = "#include\"" + std::string(HeaderName) + "\""; + std::string Content = "#include\"" + std::string(HeaderName) + + "\"\n" + "#include \"internal/internal.h\""; InMemoryFileSystem->addFile(FileName, 0, llvm::MemoryBuffer::getMemBuffer(Content)); Invocation.run(); + EXPECT_TRUE(hasSymbol(InternalSymbol)); return true; }