diff --git a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp --- a/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp +++ b/clang/lib/Tooling/Inclusions/Stdlib/StandardLibrary.cpp @@ -8,6 +8,7 @@ #include "clang/Tooling/Inclusions/StandardLibrary.h" #include "clang/AST/Decl.h" +#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Casting.h" @@ -16,8 +17,9 @@ namespace stdlib { static llvm::StringRef *HeaderNames; -static std::pair *SymbolNames; -static unsigned *SymbolHeaderIDs; +static llvm::DenseMap> + *SymbolNames; +static llvm::SmallVector *SymbolHeaderIDs; static llvm::DenseMap *HeaderIDs; // Maps symbol name -> Symbol::ID, within a namespace. using NSSymbolMap = llvm::DenseMap; @@ -29,7 +31,7 @@ #include "clang/Tooling/Inclusions/CSymbolMap.inc" #include "clang/Tooling/Inclusions/StdSymbolMap.inc" #undef SYMBOL - SymbolNames = new std::remove_reference_t[SymCount]; + SymbolNames = new std::remove_reference_t; SymbolHeaderIDs = new std::remove_reference_t[SymCount]; NamespaceSymbols = new std::remove_reference_t; @@ -46,18 +48,31 @@ return HeaderIDs->try_emplace(Header, HeaderIDs->size()).first->second; }; - auto Add = [&, SymIndex(0)](llvm::StringRef Name, llvm::StringRef NS, - llvm::StringRef HeaderName) mutable { + auto Add = [&, NextAvailSymIndex(0)](llvm::StringRef Name, llvm::StringRef NS, + llvm::StringRef HeaderName) mutable { if (NS == "None") NS = ""; - SymbolNames[SymIndex] = {NS, Name}; - SymbolHeaderIDs[SymIndex] = AddHeader(HeaderName); + bool IsNewSymbol = true; + int SymIndex = NextAvailSymIndex; + if (NSSymbolMap *NSSymbols = NamespaceSymbols->lookup(NS)) { + auto It = NSSymbols->find(Name); + if (It != NSSymbols->end()) { + SymIndex = It->getSecond(); + IsNewSymbol = false; + } + } + + SymbolNames->try_emplace(SymIndex, std::make_pair(NS, Name)); + + unsigned HeaderID = AddHeader(HeaderName); + SymbolHeaderIDs[SymIndex].emplace_back(HeaderID); NSSymbolMap &NSSymbols = AddNS(NS); NSSymbols.try_emplace(Name, SymIndex); - ++SymIndex; + if (IsNewSymbol) + ++NextAvailSymIndex; }; #define SYMBOL(Name, NS, Header) Add(#Name, #NS, #Header); #include "clang/Tooling/Inclusions/CSymbolMap.inc" @@ -84,8 +99,20 @@ return Header(It->second); } llvm::StringRef Header::name() const { return HeaderNames[ID]; } -llvm::StringRef Symbol::scope() const { return SymbolNames[ID].first; } -llvm::StringRef Symbol::name() const { return SymbolNames[ID].second; } +llvm::StringRef Symbol::scope() const { + auto It = SymbolNames->find(ID); + if (It != SymbolNames->end()) { + return It->second.first; + } + return ""; +} +llvm::StringRef Symbol::name() const { + auto It = SymbolNames->find(ID); + if (It != SymbolNames->end()) { + return It->second.second; + } + return ""; +} std::optional Symbol::named(llvm::StringRef Scope, llvm::StringRef Name) { ensureInitialized(); @@ -96,9 +123,14 @@ } return std::nullopt; } -Header Symbol::header() const { return Header(SymbolHeaderIDs[ID]); } + +Header Symbol::header() const { return Header(SymbolHeaderIDs[ID][0]); } llvm::SmallVector
Symbol::headers() const { - return {header()}; // FIXME: multiple in case of ambiguity + llvm::SmallVector
result; + for (auto HeaderID : SymbolHeaderIDs[ID]) { + result.emplace_back(Header(HeaderID)); + } + return result; } Recognizer::Recognizer() { ensureInitialized(); } diff --git a/clang/tools/include-mapping/gen_std.py b/clang/tools/include-mapping/gen_std.py --- a/clang/tools/include-mapping/gen_std.py +++ b/clang/tools/include-mapping/gen_std.py @@ -104,17 +104,12 @@ os.stat(index_page_path).st_mtime).strftime('%Y-%m-%d') print(CODE_PREFIX % (args.language.upper(), cppreference_modified_date)) for symbol in symbols: - if len(symbol.headers) == 1: - # SYMBOL(unqualified_name, namespace, header) - print("SYMBOL(%s, %s, %s)" % (symbol.name, symbol.namespace, - symbol.headers[0])) - elif len(symbol.headers) == 0: + if len(symbol.headers) == 0: sys.stderr.write("No header found for symbol %s\n" % symbol.name) else: - # FIXME: support symbols with multiple headers (e.g. std::move). - sys.stderr.write("Ambiguous header for symbol %s: %s\n" % ( - symbol.name, ', '.join(symbol.headers))) - - + for header in symbol.headers: + # SYMBOL(unqualified_name, namespace, header) + print("SYMBOL(%s, %s, %s)" % (symbol.name, symbol.namespace, + header)) if __name__ == '__main__': main()