From 5a623c2a082d007c1cc7a878b666b3581bb8f8dc Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Fri, 24 Feb 2023 01:05:15 +0100 Subject: [PATCH] Revert "Revert "[Tooling/Inclusion] Handle std::get symbol."" This reverts commit 7c9b15fbaeb2846ad25e797d55ffe1ccef9e1379. Breakage was in downstream code. --- clang-tools-extra/clangd/index/CanonicalIncludes.cpp | 3 ++- clang/include/clang/Tooling/Inclusions/StandardLibrary.h | 2 +- clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp | 11 ++++++++--- clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc | 5 +++++ clang/unittests/Tooling/StandardLibraryTest.cpp | 3 +++ 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp index 0c61a7b..4311eb9 100644 --- a/clang-tools-extra/clangd/index/CanonicalIncludes.cpp +++ b/clang-tools-extra/clangd/index/CanonicalIncludes.cpp @@ -717,7 +717,8 @@ llvm::StringRef CanonicalIncludes::mapSymbol(llvm::StringRef Scope, if (Scope == "std::" && Name == "move") return ""; if (auto StdSym = tooling::stdlib::Symbol::named(Scope, Name, Lang)) - return StdSym->header().name(); + if (auto Header = StdSym->header()) + return Header->name(); return ""; } diff --git a/clang/include/clang/Tooling/Inclusions/StandardLibrary.h b/clang/include/clang/Tooling/Inclusions/StandardLibrary.h index 9d45d84..a39ceb5 100644 --- a/clang/include/clang/Tooling/Inclusions/StandardLibrary.h +++ b/clang/include/clang/Tooling/Inclusions/StandardLibrary.h @@ -81,7 +81,7 @@ public: llvm::StringRef name() const; llvm::StringRef qualifiedName() const; // The preferred header for this symbol (e.g. the suggested insertion). - Header header() const; + std::optional
header() const; // Some symbols may be provided by multiple headers. llvm::SmallVector
headers() const; diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp index 416b531..cfcb955 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp +++ b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp @@ -14,6 +14,7 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" +#include namespace clang { namespace tooling { @@ -120,7 +121,8 @@ static int initialize(Lang Language) { } Mapping->SymbolNames[SymIndex] = { QName.data(), NSLen, static_cast(QName.size() - NSLen)}; - Mapping->SymbolHeaderIDs[SymIndex].push_back(AddHeader(HeaderName)); + if (!HeaderName.empty()) + Mapping->SymbolHeaderIDs[SymIndex].push_back(AddHeader(HeaderName)); NSSymbolMap &NSSymbols = AddNS(QName.take_front(NSLen)); NSSymbols.try_emplace(QName.drop_front(NSLen), SymIndex); @@ -205,8 +207,11 @@ std::optional Symbol::named(llvm::StringRef Scope, llvm::StringRef Name, } return std::nullopt; } -Header Symbol::header() const { - return Header(getMappingPerLang(Language)->SymbolHeaderIDs[ID][0], Language); +std::optional
Symbol::header() const { + const auto& Headers = getMappingPerLang(Language)->SymbolHeaderIDs[ID]; + if (Headers.empty()) + return std::nullopt; + return Header(Headers.front(), Language); } llvm::SmallVector
Symbol::headers() const { llvm::SmallVector
Results; diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc index 3d2ea91..c9632be 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/StdSpecialSymbolMap.inc @@ -218,3 +218,8 @@ SYMBOL(ssize, std::, ) SYMBOL(ssize, std::, ) SYMBOL(ssize, std::, ) SYMBOL(ssize, std::, ) + +// std::get has a few variants for different types (tuple, array, pair etc) +// which is tricky to disambiguate without type information. +// Don't set any header for it, as it comes with the type header. +SYMBOL(get, std::, /*no headers*/) diff --git a/clang/unittests/Tooling/StandardLibraryTest.cpp b/clang/unittests/Tooling/StandardLibraryTest.cpp index 47d064b..6d90bbd 100644 --- a/clang/unittests/Tooling/StandardLibraryTest.cpp +++ b/clang/unittests/Tooling/StandardLibraryTest.cpp @@ -58,6 +58,9 @@ TEST(StdlibTest, All) { EXPECT_EQ(Vector->header(), *VectorH); EXPECT_THAT(Vector->headers(), ElementsAre(*VectorH)); + EXPECT_TRUE(stdlib::Symbol::named("std::", "get")); + EXPECT_FALSE(stdlib::Symbol::named("std::", "get")->header()); + EXPECT_THAT(stdlib::Symbol::named("std::", "basic_iostream")->headers(), ElementsAre(stdlib::Header::named(""), stdlib::Header::named(""), -- 2.7.4