Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/AttributeCommonInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class AttributeCommonInfo {
IgnoredAttribute,
UnknownAttribute,
};
enum class Scope { NONE, CLANG, GNU, MSVC, OMP, HLSL, GSL, RISCV };

private:
const IdentifierInfo *AttrName = nullptr;
Expand Down
31 changes: 29 additions & 2 deletions clang/lib/Basic/Attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "clang/Basic/ParsedAttrInfo.h"
#include "clang/Basic/TargetInfo.h"

#include "llvm/ADT/StringMap.h"

using namespace clang;

static int hasAttributeImpl(AttributeCommonInfo::Syntax Syntax, StringRef Name,
Expand Down Expand Up @@ -153,12 +155,37 @@ std::string AttributeCommonInfo::getNormalizedFullName() const {
normalizeName(getAttrName(), getScopeName(), getSyntax()));
}

// Sorted list of attribute scope names
static constexpr std::pair<StringRef, AttributeCommonInfo::Scope> ScopeList[] =
{{"", AttributeCommonInfo::Scope::NONE},
{"clang", AttributeCommonInfo::Scope::CLANG},
{"gnu", AttributeCommonInfo::Scope::GNU},
{"gsl", AttributeCommonInfo::Scope::GSL},
{"hlsl", AttributeCommonInfo::Scope::HLSL},
{"msvc", AttributeCommonInfo::Scope::MSVC},
{"omp", AttributeCommonInfo::Scope::OMP},
{"riscv", AttributeCommonInfo::Scope::RISCV}};

AttributeCommonInfo::Scope
getScopeFromNormalizedScopeName(StringRef ScopeName) {
auto It = std::lower_bound(
std::begin(ScopeList), std::end(ScopeList), ScopeName,
[](const std::pair<StringRef, AttributeCommonInfo::Scope> &Element,
StringRef Value) { return Element.first < Value; });
assert(It != std::end(ScopeList) && It->first == ScopeName);

return It->second;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like exactly what StringSwitch is for...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooof, yeah, you're right. This should jsut be a StringSwitch...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I had no idea about StringSwitch. I'll raise a new PR to fix that.

}

unsigned AttributeCommonInfo::calculateAttributeSpellingListIndex() const {
// Both variables will be used in tablegen generated
// attribute spell list index matching code.
auto Syntax = static_cast<AttributeCommonInfo::Syntax>(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"
}
59 changes: 50 additions & 9 deletions clang/utils/TableGen/ClangAttrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/StringSwitch.h"
Expand Down Expand Up @@ -3843,19 +3844,59 @@ void EmitClangAttrSpellingListIndex(const RecordKeeper &Records,
const Record &R = *I.second;
std::vector<FlattenedSpelling> 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<StringRef> 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<StringRef, 6> SameLenNames;
llvm::copy_if(
Names, std::back_inserter(SameLenNames),
[&](StringRef N) { return N.size() == FS.name().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
Expand Down
Loading