Skip to content

Commit 68f3617

Browse files
committed
[Clang] Add __ugly__ spelling for the msvc attribute scope
1 parent d906ac5 commit 68f3617

File tree

4 files changed

+40
-33
lines changed

4 files changed

+40
-33
lines changed

clang/include/clang/Basic/Attributes.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ int hasAttribute(AttributeCommonInfo::Syntax Syntax,
2323
const IdentifierInfo *Scope, const IdentifierInfo *Attr,
2424
const TargetInfo &Target, const LangOptions &LangOpts);
2525

26+
inline const char* deuglifyAttrScope(StringRef Scope) {
27+
if (Scope == "_Clang")
28+
return "clang";
29+
if (Scope == "__gnu__")
30+
return "gnu";
31+
if (Scope == "__msvc__")
32+
return "msvc";
33+
return nullptr;
34+
}
35+
36+
inline const char* uglifyAttrScope(StringRef Scope) {
37+
if (Scope == "clang")
38+
return "_Clang";
39+
if (Scope == "gnu")
40+
return "__gnu__";
41+
if (Scope == "msvc")
42+
return "__msvc__";
43+
return nullptr;
44+
}
45+
46+
inline bool isPotentiallyUglyScope(StringRef Scope) {
47+
return Scope == "gnu" || Scope == "clang" || Scope == "msvc";
48+
}
49+
2650
} // end namespace clang
2751

2852
#endif // LLVM_CLANG_BASIC_ATTRIBUTES_H

clang/lib/Basic/Attributes.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ int clang::hasAttribute(AttributeCommonInfo::Syntax Syntax,
3838

3939
// Normalize the scope name, but only for gnu and clang attributes.
4040
StringRef ScopeName = Scope ? Scope->getName() : "";
41-
if (ScopeName == "__gnu__")
42-
ScopeName = "gnu";
43-
else if (ScopeName == "_Clang")
44-
ScopeName = "clang";
41+
if (const char *prettyName = deuglifyAttrScope(ScopeName))
42+
ScopeName = prettyName;
4543

4644
// As a special case, look for the omp::sequence and omp::directive
4745
// attributes. We support those, but not through the typical attribute
@@ -87,10 +85,8 @@ normalizeAttrScopeName(const IdentifierInfo *Scope,
8785
StringRef ScopeName = Scope->getName();
8886
if (SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
8987
SyntaxUsed == AttributeCommonInfo::AS_C23) {
90-
if (ScopeName == "__gnu__")
91-
ScopeName = "gnu";
92-
else if (ScopeName == "_Clang")
93-
ScopeName = "clang";
88+
if (const char *prettySpelling = deuglifyAttrScope(ScopeName))
89+
return prettySpelling;
9490
}
9591
return ScopeName;
9692
}
@@ -100,12 +96,11 @@ static StringRef normalizeAttrName(const IdentifierInfo *Name,
10096
AttributeCommonInfo::Syntax SyntaxUsed) {
10197
// Normalize the attribute name, __foo__ becomes foo. This is only allowable
10298
// for GNU attributes, and attributes using the double square bracket syntax.
103-
bool ShouldNormalize =
104-
SyntaxUsed == AttributeCommonInfo::AS_GNU ||
105-
((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
106-
SyntaxUsed == AttributeCommonInfo::AS_C23) &&
107-
(NormalizedScopeName.empty() || NormalizedScopeName == "gnu" ||
108-
NormalizedScopeName == "clang"));
99+
bool ShouldNormalize = SyntaxUsed == AttributeCommonInfo::AS_GNU ||
100+
((SyntaxUsed == AttributeCommonInfo::AS_CXX11 ||
101+
SyntaxUsed == AttributeCommonInfo::AS_C23) &&
102+
(NormalizedScopeName.empty() ||
103+
isPotentiallyUglyScope(NormalizedScopeName)));
109104
StringRef AttrName = Name->getName();
110105
if (ShouldNormalize && AttrName.size() >= 4 && AttrName.starts_with("__") &&
111106
AttrName.ends_with("__"))

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "clang/AST/RecursiveASTVisitor.h"
2626
#include "clang/AST/Type.h"
2727
#include "clang/Basic/AttributeCommonInfo.h"
28+
#include "clang/Basic/Attributes.h"
2829
#include "clang/Basic/CharInfo.h"
2930
#include "clang/Basic/OperatorKinds.h"
3031
#include "clang/Basic/Specifiers.h"
@@ -4579,22 +4580,6 @@ void SemaCodeCompletion::CodeCompleteDeclSpec(Scope *S, DeclSpec &DS,
45794580
Results.size());
45804581
}
45814582

4582-
static const char *underscoreAttrScope(llvm::StringRef Scope) {
4583-
if (Scope == "clang")
4584-
return "_Clang";
4585-
if (Scope == "gnu")
4586-
return "__gnu__";
4587-
return nullptr;
4588-
}
4589-
4590-
static const char *noUnderscoreAttrScope(llvm::StringRef Scope) {
4591-
if (Scope == "_Clang")
4592-
return "clang";
4593-
if (Scope == "__gnu__")
4594-
return "gnu";
4595-
return nullptr;
4596-
}
4597-
45984583
void SemaCodeCompletion::CodeCompleteAttribute(
45994584
AttributeCommonInfo::Syntax Syntax, AttributeCompletion Completion,
46004585
const IdentifierInfo *InScope) {
@@ -4618,7 +4603,7 @@ void SemaCodeCompletion::CodeCompleteAttribute(
46184603
bool InScopeUnderscore = false;
46194604
if (InScope) {
46204605
InScopeName = InScope->getName();
4621-
if (const char *NoUnderscore = noUnderscoreAttrScope(InScopeName)) {
4606+
if (const char *NoUnderscore = deuglifyAttrScope(InScopeName)) {
46224607
InScopeName = NoUnderscore;
46234608
InScopeUnderscore = true;
46244609
}
@@ -4653,7 +4638,7 @@ void SemaCodeCompletion::CodeCompleteAttribute(
46534638
Results.AddResult(
46544639
CodeCompletionResult(Results.getAllocator().CopyString(Scope)));
46554640
// Include alternate form (__gnu__ instead of gnu).
4656-
if (const char *Scope2 = underscoreAttrScope(Scope))
4641+
if (const char *Scope2 = uglifyAttrScope(Scope))
46574642
Results.AddResult(CodeCompletionResult(Scope2));
46584643
}
46594644
continue;
@@ -4712,7 +4697,7 @@ void SemaCodeCompletion::CodeCompleteAttribute(
47124697
if (Scope.empty()) {
47134698
Add(Scope, Name, /*Underscores=*/true);
47144699
} else {
4715-
const char *GuardedScope = underscoreAttrScope(Scope);
4700+
const char *GuardedScope = uglifyAttrScope(Scope);
47164701
if (!GuardedScope)
47174702
continue;
47184703
Add(GuardedScope, Name, /*Underscores=*/true);

clang/test/SemaCXX/cxx2a-ms-no-unique-address.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ struct [[msvc::no_unique_address]] S { // expected-error {{only applies to non-b
1616
[[msvc::no_unique_address()]] int arglist; // expected-error {{cannot have an argument list}} unsupported-warning {{unknown}}
1717

1818
int [[msvc::no_unique_address]] c; // expected-error {{cannot be applied to types}} unsupported-error {{cannot be applied to types}}
19+
[[__msvc__::__no_unique_address__]] int d; // unsupported-warning {{unknown}}
20+
[[__msvc__::no_unique_address]] int e; // unsupported-warning {{unknown}}
21+
[[msvc::__no_unique_address__]] int g; // unsupported-warning {{unknown}}
1922
};
2023

2124
struct CStructNoUniqueAddress {

0 commit comments

Comments
 (0)