[clang-tools-extra] 58927e9 - [clangd] Remove the direct use of StdSymbolMapping.inc usage.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 6 05:43:18 PST 2023
Author: Haojian Wu
Date: 2023-02-06T14:41:52+01:00
New Revision: 58927e9931219387895b4ef67ebb50eafa2d9056
URL: https://github.com/llvm/llvm-project/commit/58927e9931219387895b4ef67ebb50eafa2d9056
DIFF: https://github.com/llvm/llvm-project/commit/58927e9931219387895b4ef67ebb50eafa2d9056.diff
LOG: [clangd] Remove the direct use of StdSymbolMapping.inc usage.
Replace them with the library APIs.
Differential Revision: https://reviews.llvm.org/D143274
Added:
Modified:
clang-tools-extra/clangd/index/CanonicalIncludes.cpp
clang-tools-extra/clangd/index/CanonicalIncludes.h
clang-tools-extra/clangd/index/StdLib.cpp
clang-tools-extra/clangd/index/SymbolCollector.cpp
clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
index f123b029f9fb3..41d195f81298e 100644
--- a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
+++ b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp
@@ -10,6 +10,7 @@
#include "Headers.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Tooling/Inclusions/HeaderAnalysis.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem/UniqueID.h"
#include "llvm/Support/Path.h"
@@ -700,8 +701,26 @@ llvm::StringRef CanonicalIncludes::mapHeader(FileEntryRef Header) const {
return "";
}
-llvm::StringRef CanonicalIncludes::mapSymbol(llvm::StringRef QName) const {
- return StdSymbolMapping ? StdSymbolMapping->lookup(QName) : "";
+llvm::StringRef CanonicalIncludes::mapSymbol(llvm::StringRef Scope,
+ llvm::StringRef Name,
+ const LangOptions &L) const {
+ tooling::stdlib::Lang Lang;
+ if (L.CPlusPlus)
+ Lang = tooling::stdlib::Lang::CXX;
+ else if (L.C11)
+ Lang = tooling::stdlib::Lang::C;
+ else
+ return "";
+ // FIXME: remove the following special cases when the tooling stdlib supports
+ // them.
+ // There are two std::move()s, this is by far the most common.
+ if (Scope == "std::" && Name == "move")
+ return "<utility>";
+ if (Scope == "std::" && Name == "size_t")
+ return "<cstddef>";
+ if (auto StdSym = tooling::stdlib::Symbol::named(Scope, Name, Lang))
+ return StdSym->header().name();
+ return "";
}
std::unique_ptr<CommentHandler>
@@ -732,28 +751,6 @@ collectIWYUHeaderMaps(CanonicalIncludes *Includes) {
}
void CanonicalIncludes::addSystemHeadersMapping(const LangOptions &Language) {
- if (Language.CPlusPlus) {
- static const auto *Symbols = new llvm::StringMap<llvm::StringRef>({
-#define SYMBOL(Name, NameSpace, Header) {#NameSpace #Name, #Header},
-#include "clang/Tooling/Inclusions/StdSymbolMap.inc"
- // There are two std::move()s, this is by far the most common.
- SYMBOL(move, std::, <utility>)
- // There are multiple headers for size_t, pick one.
- SYMBOL(size_t, std::, <cstddef>)
-#undef SYMBOL
- });
- StdSymbolMapping = Symbols;
- } else if (Language.C11) {
- static const auto *CSymbols = new llvm::StringMap<llvm::StringRef>({
-#define SYMBOL(Name, NameSpace, Header) {#Name, #Header},
-#include "clang/Tooling/Inclusions/CSymbolMap.inc"
- // There are multiple headers for size_t, pick one.
- SYMBOL(size_t, None, <stddef.h>)
-#undef SYMBOL
- });
- StdSymbolMapping = CSymbols;
- }
-
// FIXME: remove the std header mapping once we support ambiguous symbols, now
// it serves as a fallback to disambiguate:
// - symbols with multiple headers (e.g. std::move)
diff --git a/clang-tools-extra/clangd/index/CanonicalIncludes.h b/clang-tools-extra/clangd/index/CanonicalIncludes.h
index f6ac4a1fb271f..f1464885018e9 100644
--- a/clang-tools-extra/clangd/index/CanonicalIncludes.h
+++ b/clang-tools-extra/clangd/index/CanonicalIncludes.h
@@ -39,8 +39,12 @@ class CanonicalIncludes {
/// Adds a file-to-string mapping from \p ID to \p CanonicalPath.
void addMapping(FileEntryRef Header, llvm::StringRef CanonicalPath);
- /// Returns the overridden include for symbol with \p QualifiedName, or "".
- llvm::StringRef mapSymbol(llvm::StringRef QualifiedName) const;
+ /// Returns the overridden include for a qualified symbol with, or "".
+ /// \p Scope and \p Name concatenation forms the fully qualified name.
+ /// \p Scope is the qualifier with the trailing "::" (e.g. "std::") or empty
+ /// (for global namespace).
+ llvm::StringRef mapSymbol(llvm::StringRef Scope, llvm::StringRef Name,
+ const LangOptions &L) const;
/// Returns the overridden include for files in \p Header, or "".
llvm::StringRef mapHeader(FileEntryRef Header) const;
@@ -61,9 +65,6 @@ class CanonicalIncludes {
/// A map from a suffix (one or components of a path) to a canonical path.
/// Used only for mapping standard headers.
const llvm::StringMap<llvm::StringRef> *StdSuffixHeaderMapping = nullptr;
- /// A map from fully qualified symbol names to header names.
- /// Used only for mapping standard symbols.
- const llvm::StringMap<llvm::StringRef> *StdSymbolMapping = nullptr;
};
/// Returns a CommentHandler that parses pragma comment on include files to
diff --git a/clang-tools-extra/clangd/index/StdLib.cpp b/clang-tools-extra/clangd/index/StdLib.cpp
index 399bd2ffaff6b..1133d984efbae 100644
--- a/clang-tools-extra/clangd/index/StdLib.cpp
+++ b/clang-tools-extra/clangd/index/StdLib.cpp
@@ -22,6 +22,7 @@
#include "clang/Basic/LangOptions.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Lex/PreprocessorOptions.h"
+#include "clang/Tooling/Inclusions/StandardLibrary.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
@@ -67,7 +68,7 @@ LangStandard::Kind standardFromOpts(const LangOptions &LO) {
}
std::string buildUmbrella(llvm::StringLiteral Mandatory,
- std::vector<llvm::StringLiteral> Headers) {
+ llvm::ArrayRef<tooling::stdlib::Header> Headers) {
std::string Result;
llvm::raw_string_ostream OS(Result);
@@ -80,13 +81,11 @@ std::string buildUmbrella(llvm::StringLiteral Mandatory,
"#endif\n",
Mandatory);
- llvm::sort(Headers);
- auto Last = std::unique(Headers.begin(), Headers.end());
- for (auto Header = Headers.begin(); Header != Last; ++Header) {
+ for (auto Header : Headers) {
OS << llvm::formatv("#if __has_include({0})\n"
"#include {0}\n"
"#endif\n",
- *Header);
+ Header);
}
OS.flush();
return Result;
@@ -102,20 +101,14 @@ llvm::StringRef getStdlibUmbrellaHeader(const LangOptions &LO) {
Lang L = langFromOpts(LO);
switch (L) {
case CXX:
- static std::string *UmbrellaCXX =
- new std::string(buildUmbrella(mandatoryHeader(L), {
-#define SYMBOL(Name, NameSpace, Header) #Header,
-#include "clang/Tooling/Inclusions/StdSymbolMap.inc"
-#undef SYMBOL
- }));
+ static std::string *UmbrellaCXX = new std::string(buildUmbrella(
+ mandatoryHeader(L),
+ tooling::stdlib::Header::all(tooling::stdlib::Lang::CXX)));
return *UmbrellaCXX;
case C:
- static std::string *UmbrellaC =
- new std::string(buildUmbrella(mandatoryHeader(L), {
-#define SYMBOL(Name, NameSpace, Header) #Header,
-#include "clang/Tooling/Inclusions/CSymbolMap.inc"
-#undef SYMBOL
- }));
+ static std::string *UmbrellaC = new std::string(
+ buildUmbrella(mandatoryHeader(L),
+ tooling::stdlib::Header::all(tooling::stdlib::Lang::C)));
return *UmbrellaC;
}
llvm_unreachable("invalid Lang in langFromOpts");
@@ -141,13 +134,10 @@ SymbolSlab filter(SymbolSlab Slab, const StdLibLocation &Loc) {
static auto &StandardHeaders = *[] {
auto *Set = new llvm::DenseSet<llvm::StringRef>();
- for (llvm::StringRef Header : {
-#define SYMBOL(Name, NameSpace, Header) #Header,
-#include "clang/Tooling/Inclusions/CSymbolMap.inc"
-#include "clang/Tooling/Inclusions/StdSymbolMap.inc"
-#undef SYMBOL
- })
- Set->insert(Header);
+ for (auto Header : tooling::stdlib::Header::all(tooling::stdlib::Lang::CXX))
+ Set->insert(Header.name());
+ for (auto Header : tooling::stdlib::Header::all(tooling::stdlib::Lang::C))
+ Set->insert(Header.name());
return Set;
}();
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 822f3cb9b9563..24d01043a42d6 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -829,20 +829,18 @@ void SymbolCollector::finish() {
llvm::DenseMap<FileID, bool> FileToContainsImportsOrObjC;
// Fill in IncludeHeaders.
// We delay this until end of TU so header guards are all resolved.
- llvm::SmallString<128> QName;
for (const auto &[SID, FID] : IncludeFiles) {
if (const Symbol *S = Symbols.find(SID)) {
llvm::StringRef IncludeHeader;
// Look for an overridden include header for this symbol specifically.
if (Opts.Includes) {
- QName = S->Scope;
- QName.append(S->Name);
- IncludeHeader = Opts.Includes->mapSymbol(QName);
+ IncludeHeader =
+ Opts.Includes->mapSymbol(S->Scope, S->Name, ASTCtx->getLangOpts());
if (!IncludeHeader.empty()) {
if (IncludeHeader.front() != '"' && IncludeHeader.front() != '<')
IncludeHeader = HeaderFileURIs->toURI(IncludeHeader);
- else if (IncludeHeader == "<utility>" && QName == "std::move" &&
- S->Signature.contains(','))
+ else if (IncludeHeader == "<utility>" && S->Scope == "std::" &&
+ S->Name == "move" && S->Signature.contains(','))
IncludeHeader = "<algorithm>";
}
}
diff --git a/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp b/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
index 0329cdc50cad2..dfde5594956ef 100644
--- a/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
+++ b/clang-tools-extra/clangd/unittests/CanonicalIncludesTests.cpp
@@ -36,8 +36,8 @@ TEST(CanonicalIncludesTest, CStandardLibrary) {
Language.C11 = true;
CI.addSystemHeadersMapping(Language);
// Usual standard library symbols are mapped correctly.
- EXPECT_EQ("<stdio.h>", CI.mapSymbol("printf"));
- EXPECT_EQ("", CI.mapSymbol("unknown_symbol"));
+ EXPECT_EQ("<stdio.h>", CI.mapSymbol("", "printf", Language));
+ EXPECT_EQ("", CI.mapSymbol("", "unknown_symbol", Language));
}
TEST(CanonicalIncludesTest, CXXStandardLibrary) {
@@ -47,14 +47,14 @@ TEST(CanonicalIncludesTest, CXXStandardLibrary) {
CI.addSystemHeadersMapping(Language);
// Usual standard library symbols are mapped correctly.
- EXPECT_EQ("<vector>", CI.mapSymbol("std::vector"));
- EXPECT_EQ("<cstdio>", CI.mapSymbol("std::printf"));
+ EXPECT_EQ("<vector>", CI.mapSymbol("std::", "vector", Language));
+ EXPECT_EQ("<cstdio>", CI.mapSymbol("std::", "printf", Language));
// std::move is ambiguous, currently always mapped to <utility>
- EXPECT_EQ("<utility>", CI.mapSymbol("std::move"));
+ EXPECT_EQ("<utility>", CI.mapSymbol("std::", "move", Language));
// Unknown std symbols aren't mapped.
- EXPECT_EQ("", CI.mapSymbol("std::notathing"));
+ EXPECT_EQ("", CI.mapSymbol("std::", "notathing", Language));
// iosfwd declares some symbols it doesn't own.
- EXPECT_EQ("<ostream>", CI.mapSymbol("std::ostream"));
+ EXPECT_EQ("<ostream>", CI.mapSymbol("std::", "ostream", Language));
// And (for now) we assume it owns the others.
auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
FileManager Files(FileSystemOptions(), InMemFS);
More information about the cfe-commits
mailing list