From ed8d55718f937d532ed9eb52e7c3ce86c4600652 Mon Sep 17 00:00:00 2001 From: Zahira Ammarguellat Date: Tue, 31 Dec 2024 07:03:19 -0800 Subject: [PATCH] Reland https://github.com/llvm/llvm-project/pull/114899 --- .../include/clang/Basic/AttributeCommonInfo.h | 14 +++++ clang/lib/Basic/Attributes.cpp | 26 ++++++++- clang/utils/TableGen/ClangAttrEmitter.cpp | 58 ++++++++++++++++--- 3 files changed, 87 insertions(+), 11 deletions(-) diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 5f024b4b5fd78..04b68c5a1d8dd 100644 --- a/clang/include/clang/Basic/AttributeCommonInfo.h +++ b/clang/include/clang/Basic/AttributeCommonInfo.h @@ -67,6 +67,20 @@ class AttributeCommonInfo { IgnoredAttribute, UnknownAttribute, }; + enum class Scope { + NONE, + CLANG, + GNU, + MSVC, + OMP, + HLSL, + GSL, + RISCV, + INTEL, + SYCL, + CL, + SYCL_DETAIL + }; private: const IdentifierInfo *AttrName = nullptr; diff --git a/clang/lib/Basic/Attributes.cpp b/clang/lib/Basic/Attributes.cpp index 9577277d85f81..960a7f7bc18af 100644 --- a/clang/lib/Basic/Attributes.cpp +++ b/clang/lib/Basic/Attributes.cpp @@ -17,6 +17,9 @@ #include "clang/Basic/ParsedAttrInfo.h" #include "clang/Basic/TargetInfo.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringSwitch.h" + using namespace clang; static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name, @@ -156,13 +159,32 @@ std::string AttributeCommonInfo::getNormalizedFullName() const { return static_cast( normalizeName(getAttrName(), getScopeName(), getSyntax())); } +static AttributeCommonInfo::Scope +getScopeFromNormalizedScopeName(StringRef ScopeName) { + return llvm::StringSwitch(ScopeName) + .Case("", AttributeCommonInfo::Scope::NONE) + .Case("clang", AttributeCommonInfo::Scope::CLANG) + .Case("gnu", AttributeCommonInfo::Scope::GNU) + .Case("gsl", AttributeCommonInfo::Scope::GSL) + .Case("hlsl", AttributeCommonInfo::Scope::HLSL) + .Case("msvc", AttributeCommonInfo::Scope::MSVC) + .Case("omp", AttributeCommonInfo::Scope::OMP) + .Case("riscv", AttributeCommonInfo::Scope::RISCV) + .Case("intel", AttributeCommonInfo::Scope::INTEL) + .Case("sycl", AttributeCommonInfo::Scope::SYCL) + .Case("cl", AttributeCommonInfo::Scope::CL) + .Case("__sycl_detail__", AttributeCommonInfo::Scope::SYCL_DETAIL); +} unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const { // Both variables will be used in tablegen generated // attribute spell list index matching code. auto Syntax = static_cast(getSyntax()); - StringRef Scope = normalizeAttrScopeName(getScopeName(), Syntax); - StringRef Name = normalizeAttrName(getAttrName(), Scope, Syntax); + StringRef ScopeName = normalizeAttrScopeName(getScopeName(), Syntax); + StringRef Name = normalizeAttrName(getAttrName(), ScopeName, Syntax); + + AttributeCommonInfo::Scope ComputedScope = + getScopeFromNormalizedScopeName(ScopeName); #include "clang/Sema/AttrSpellingListIndex.inc" } diff --git a/clang/utils/TableGen/ClangAttrEmitter.cpp b/clang/utils/TableGen/ClangAttrEmitter.cpp index ebc1db374c775..ca6398b2c4a7c 100644 --- a/clang/utils/TableGen/ClangAttrEmitter.cpp +++ b/clang/utils/TableGen/ClangAttrEmitter.cpp @@ -3851,19 +3851,59 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper &Records, const Record &R = *I.second; std::vector Spellings = GetFlattenedSpellings(R); OS << " case AT_" << I.first << ": {\n"; - for (unsigned I = 0; I < Spellings.size(); ++ I) { - OS << " if (Name == \"" << Spellings[I].name() << "\" && " - << "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety() - << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n" - << " return " << I << ";\n"; + + // If there are none or one spelling to check, resort to the default + // behavior of returning index as 0. + if (Spellings.size() <= 1) { + OS << " return 0;\n" + << " break;\n" + << " }\n"; + continue; } - OS << " break;\n"; - OS << " }\n"; + std::vector Names; + llvm::transform(Spellings, std::back_inserter(Names), + [](const FlattenedSpelling &FS) { return FS.name(); }); + llvm::sort(Names); + Names.erase(llvm::unique(Names), Names.end()); + + for (const auto &[Idx, FS] : enumerate(Spellings)) { + OS << " if ("; + if (Names.size() > 1) { + SmallVector SameLenNames; + StringRef FSName = FS.name(); + llvm::copy_if(Names, std::back_inserter(SameLenNames), + [&](StringRef N) { return N.size() == FSName.size(); }); + + if (SameLenNames.size() == 1) { + OS << "Name.size() == " << FS.name().size() << " && "; + } else { + // FIXME: We currently fall back to comparing entire strings if there + // are 2 or more spelling names with the same length. This can be + // optimized to check only for the the first differing character + // between them instead. + OS << "Name == \"" << FS.name() << "\"" + << " && "; + } + } + + OS << "getSyntax() == AttributeCommonInfo::AS_" << FS.variety() + << " && ComputedScope == "; + if (FS.nameSpace() == "") + OS << "AttributeCommonInfo::Scope::NONE"; + else + OS << "AttributeCommonInfo::Scope::" + FS.nameSpace().upper(); + + OS << ")\n" + << " return " << Idx << ";\n"; + } + + OS << " break;\n" + << " }\n"; } - OS << " }\n"; - OS << " return 0;\n"; + OS << " }\n" + << " return 0;\n"; } // Emits code used by RecursiveASTVisitor to visit attributes