[clangd] Disable suffix matching fallback for C during include insertion

Clangd currently doesn't respect language and breaks the builds with
include insertion for C. This patch aims to stop the bleeding by not mapping
back to CPP standard library headers.

Improves https://github.com/clangd/clangd/issues/376.

Differential Revision: https://reviews.llvm.org/D88144
This commit is contained in:
Kadir Cetinkaya 2020-09-23 14:28:57 +02:00
parent 27da287507
commit 64168c6d99
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
2 changed files with 8 additions and 1 deletions

View File

@ -772,7 +772,10 @@ void CanonicalIncludes::addSystemHeadersMapping(const LangOptions &Language) {
MaxSuffixComponents; MaxSuffixComponents;
}) != SystemHeaderMap->keys().end()); }) != SystemHeaderMap->keys().end());
StdSuffixHeaderMapping = SystemHeaderMap; // FIXME: Suffix mapping contains invalid entries for C, so only enable it for
// CPP.
if (Language.CPlusPlus)
StdSuffixHeaderMapping = SystemHeaderMap;
} }
} // namespace clangd } // namespace clangd

View File

@ -21,6 +21,10 @@ TEST(CanonicalIncludesTest, CStandardLibrary) {
CI.addSystemHeadersMapping(Language); CI.addSystemHeadersMapping(Language);
// Usual standard library symbols are mapped correctly. // Usual standard library symbols are mapped correctly.
EXPECT_EQ("<stdio.h>", CI.mapHeader("path/stdio.h", "printf")); EXPECT_EQ("<stdio.h>", CI.mapHeader("path/stdio.h", "printf"));
// Suffix mapping isn't available for C, instead of mapping to `<cstdio> we
// just leave the header as-is.
EXPECT_EQ("include/stdio.h",
CI.mapHeader("include/stdio.h", "unknown_symbol"));
} }
TEST(CanonicalIncludesTest, CXXStandardLibrary) { TEST(CanonicalIncludesTest, CXXStandardLibrary) {