diff --git a/clang-tools-extra/clangd/index/CanonicalIncludes.h b/clang-tools-extra/clangd/index/CanonicalIncludes.h --- a/clang-tools-extra/clangd/index/CanonicalIncludes.h +++ b/clang-tools-extra/clangd/index/CanonicalIncludes.h @@ -44,10 +44,10 @@ llvm::StringRef mapHeader(llvm::StringRef Header, llvm::StringRef QualifiedName) const; - /// Adds mapping for system headers and some special symbols (e.g. STL symbols - /// in need to be mapped individually). Approximately, the following - /// system headers are handled: - /// - C++ standard library e.g. bits/basic_string.h$ -> + /// Adds mapping for system headers and some special symbols. Uses a symbol + /// based mapping for STL symbols. Handles other libraries by matching on the + /// path. Approximately, the following system headers are handled: + /// - C++ standard library e.g. std::string -> /// - Posix library e.g. bits/pthreadtypes.h$ -> /// - Compiler extensions, e.g. include/avx512bwintrin.h$ -> /// The mapping is hardcoded and hand-maintained, so it might not cover all diff --git a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp --- a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp +++ b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp @@ -108,7 +108,11 @@ // it serves as a fallback to disambiguate: // - symbols with multiple headers (e.g. std::move) static const auto *SystemHeaderMap = new llvm::StringMap({ + // Resource lib {"include/__stddef_max_align_t.h", ""}, + {"include/_G_config.h", ""}, + {"include/assert.h", ""}, + // Compiler intrinsics {"include/__wmmintrin_aes.h", ""}, {"include/__wmmintrin_pclmul.h", ""}, {"include/adxintrin.h", ""}, @@ -170,281 +174,6 @@ {"include/xsaveoptintrin.h", ""}, {"include/xsavesintrin.h", ""}, {"include/xtestintrin.h", ""}, - {"include/_G_config.h", ""}, - {"include/assert.h", ""}, - {"algorithm", ""}, - {"valarray", ""}, - {"array", ""}, - {"atomic", ""}, - {"backward/auto_ptr.h", ""}, - {"backward/binders.h", ""}, - {"bits/algorithmfwd.h", ""}, - {"bits/alloc_traits.h", ""}, - {"bits/allocated_ptr.h", ""}, - {"bits/allocator.h", ""}, - {"bits/atomic_base.h", ""}, - {"bits/atomic_lockfree_defines.h", ""}, - {"bits/atomic_futex.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.h", ""}, - {"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/invoke.h", ""}, - {"bits/ios_base.h", ""}, - {"bits/istream.tcc", ""}, - {"bits/list.tcc", ""}, - {"bits/locale_classes.h", ""}, - {"bits/locale_classes.tcc", ""}, - {"bits/locale_conv.h", ""}, - {"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/parse_numbers.h", ""}, - {"bits/postypes.h", ""}, - {"bits/predefined_ops.h", ""}, - {"bits/ptr_traits.h", ""}, - {"bits/quoted_string.h", ""}, - {"bits/random.h", ""}, - {"bits/random.tcc", ""}, - {"bits/range_access.h", ""}, - {"bits/refwrap.h", ""}, - {"bits/regex.h", ""}, - {"bits/regex_automaton.h", ""}, - {"bits/regex_compiler.h", ""}, - {"bits/regex_constants.h", ""}, - {"bits/regex_cursor.h", ""}, - {"bits/regex_error.h", ""}, - {"bits/regex_executor.h", ""}, - {"bits/regex_grep_matcher.h", ""}, - {"bits/regex_grep_matcher.tcc", ""}, - {"bits/regex_nfa.h", ""}, - {"bits/regex_scanner.h", ""}, - {"bits/shared_ptr.h", ""}, - {"bits/shared_ptr_base.h", ""}, - {"bits/shared_ptr_atomic.h", ""}, - {"bits/slice_array.h", ""}, - {"bits/sstream.tcc", ""}, - {"bits/std_abs.h", ""}, - {"bits/std_function.h", ""}, - {"bits/std_mutex.h", ""}, - {"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/uniform_int_dist.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", ""}, - {"debug/map.h", ""}, - {"debug/multimap.h", ""}, - {"debug/multiset.h", ""}, - {"debug/set.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", ""}, - {"shared_mutex", ""}, - {"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/errno.h", ""}, - {"include/fenv.h", ""}, - {"include/inttypes.h", ""}, - {"include/libio.h", ""}, - {"include/limits.h", ""}, - {"include/locale.h", ""}, - {"include/math.h", ""}, - {"include/setjmp.h", ""}, - {"include/signal.h", ""}, - {"include/stdint.h", ""}, - {"include/stdio.h", ""}, - {"include/stdlib.h", ""}, - {"include/string.h", ""}, - {"include/time.h", ""}, - {"include/wchar.h", ""}, - {"include/wctype.h", ""}, - {"bits/cmathcalls.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/setjmp.h", ""}, - {"bits/sigaction.h", ""}, - {"bits/sigcontext.h", ""}, - {"bits/siginfo.h", ""}, - {"bits/signum.h", ""}, - {"bits/sigset.h", ""}, - {"bits/sigstack.h", ""}, - {"bits/stdint-intn.h", ""}, - {"bits/stdint-uintn.h", ""}, - {"bits/stdio_lim.h", ""}, - {"bits/sys_errlist.h", ""}, - {"bits/time.h", ""}, - {"bits/timex.h", ""}, - {"bits/typesizes.h", ""}, - {"bits/wchar.h", ""}, - {"bits/wordsize.h", ""}, - {"bits/xopen_lim.h", ""}, - {"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", ""}, // GNU C headers {"include/aio.h", ""}, {"include/aliases.h", ""}, diff --git a/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp b/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp --- a/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp +++ b/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp @@ -21,10 +21,6 @@ CI.addSystemHeadersMapping(Language); // Usual standard library symbols are mapped correctly. EXPECT_EQ("", CI.mapHeader("path/stdio.h", "printf")); - // Suffix mapping isn't available for C, instead of mapping to ` we - // just leave the header as-is. - EXPECT_EQ("include/stdio.h", - CI.mapHeader("include/stdio.h", "unknown_symbol")); } TEST(CanonicalIncludesTest, CXXStandardLibrary) { @@ -34,17 +30,12 @@ CI.addSystemHeadersMapping(Language); // Usual standard library symbols are mapped correctly. - EXPECT_EQ("", CI.mapHeader("path/vector.h", "std::vector")); - EXPECT_EQ("", CI.mapHeader("path/stdio.h", "std::printf")); - // std::move is ambiguous, currently always mapped to - EXPECT_EQ("", - CI.mapHeader("libstdc++/bits/stl_algo.h", "std::move")); + EXPECT_EQ("", CI.mapHeader("some_lib.h", "std::vector")); + EXPECT_EQ("", CI.mapHeader("some_lib.h", "std::printf")); + // std::move is ambiguous, always map to . + EXPECT_EQ("", CI.mapHeader("some_lib.h", "std::move")); // Unknown std symbols aren't mapped. EXPECT_EQ("foo/bar.h", CI.mapHeader("foo/bar.h", "std::notathing")); - // iosfwd declares some symbols it doesn't own. - EXPECT_EQ("", CI.mapHeader("iosfwd", "std::ostream")); - // And (for now) we assume it owns the others. - EXPECT_EQ("", CI.mapHeader("iosfwd", "std::notwathing")); } TEST(CanonicalIncludesTest, PathMapping) { @@ -77,8 +68,6 @@ // We added a mapping from some/path to . ASSERT_EQ("", CI.mapHeader("some/path", "")); - // We should have a path from 'bits/stl_vector.h' to ''. - ASSERT_EQ("", CI.mapHeader("bits/stl_vector.h", "")); // We should also have a symbol mapping from 'std::map' to ''. ASSERT_EQ("", CI.mapHeader("some/header.h", "std::map")); diff --git a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/FileIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/FileIndexTests.cpp @@ -226,10 +226,11 @@ TEST(FileIndexTest, HasSystemHeaderMappingsInPreamble) { TestTU TU; - TU.HeaderCode = "class Foo{};"; - TU.HeaderFilename = "algorithm"; + // std::max is mapped back to + TU.HeaderCode = "namespace std { void max(); }"; + TU.HeaderFilename = "ignored_header_name.h"; - auto Symbols = runFuzzyFind(*TU.index(), ""); + auto Symbols = runFuzzyFind(*TU.index(), "max"); EXPECT_THAT(Symbols, ElementsAre(_)); EXPECT_THAT(Symbols.begin()->IncludeHeaders.front().IncludeHeader, "");