]> granicus.if.org Git - clang/commitdiff
Allow a double-underscore spelling of Clang attributes using double square bracket...
authorAaron Ballman <aaron@aaronballman.com>
Fri, 9 Nov 2018 19:37:18 +0000 (19:37 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Fri, 9 Nov 2018 19:37:18 +0000 (19:37 +0000)
This matches a similar behavior with GCC accepting [[gnu::__attr__]] as a alias for [[gnu::attr]] in that clang attributes can now be spelled with two leading and trailing underscores.

I had always intended for this to work, but missed the critical bit. We already had an existing test in test/Preprocessor/has_attribute.cpp for [[clang::__fallthrough__]] but using that spelling would still give an "unknown attribute" diagnostic.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346547 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/ParsedAttr.cpp
test/SemaCXX/attr-optnone.cpp
test/SemaCXX/switch-implicit-fallthrough.cpp

index 4c46063e2c18e57a6a32facfad7266bdb5a8f918..59e5aab677a9e5f85e9683f1a95e11dd86157b56 100644 (file)
@@ -122,11 +122,12 @@ static StringRef normalizeAttrName(StringRef AttrName,
                                    ParsedAttr::Syntax SyntaxUsed) {
   // Normalize the attribute name, __foo__ becomes foo. This is only allowable
   // for GNU attributes, and attributes using the double square bracket syntax.
-  bool IsGNU = SyntaxUsed == ParsedAttr::AS_GNU ||
-               ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
-                 SyntaxUsed == ParsedAttr::AS_C2x) &&
-                NormalizedScopeName == "gnu");
-  if (IsGNU && AttrName.size() >= 4 && AttrName.startswith("__") &&
+  bool ShouldNormalize =
+      SyntaxUsed == ParsedAttr::AS_GNU ||
+      ((SyntaxUsed == ParsedAttr::AS_CXX11 ||
+        SyntaxUsed == ParsedAttr::AS_C2x) &&
+       (NormalizedScopeName == "gnu" || NormalizedScopeName == "clang"));
+  if (ShouldNormalize && AttrName.size() >= 4 && AttrName.startswith("__") &&
       AttrName.endswith("__"))
     AttrName = AttrName.slice(2, AttrName.size() - 2);
 
index 1e02653b95ead8180eeff03e2606a01444134e1b..c8e85eec371163e3b9ff2797a7add2ef0b5a9702 100644 (file)
@@ -72,8 +72,10 @@ struct B2 {
 };
 
 // Verify that we can handle the [[_Clang::optnone]] and
-// [[__clang__::optnone]] spellings.
+// [[__clang__::optnone]] spellings, as well as [[clang::__optnone__]].
 [[_Clang::optnone]] int foo3();
 [[__clang__::optnone]] int foo4(); // expected-warning {{'__clang__' is a predefined macro name, not an attribute scope specifier; did you mean '_Clang' instead?}}
+[[clang::__optnone__]] int foo5();
+[[_Clang::__optnone__]] int foo6();
 
-[[_Clang::optnone]] int foo5; // expected-warning {{'optnone' attribute only applies to functions}}
+[[_Clang::optnone]] int foo7; // expected-warning {{'optnone' attribute only applies to functions}}
index 9540b1ff28808bb3214ccc8870e9e8090a9f782b..6ccac122cff73c33df970cd7d4099aa9f7c39fb0 100644 (file)
@@ -314,3 +314,18 @@ int fallthrough_targets(int n) {
   }
   return n;
 }
+
+int fallthrough_alt_spelling(int n) {
+  switch (n) {
+  case 0:
+    n++;
+    [[clang::fallthrough]];
+  case 1:
+    n++;
+    [[clang::__fallthrough__]];
+  case 2:
+    n++;
+    break;
+  }
+  return n;
+}