r346547 - Allow a double-underscore spelling of Clang attributes using double square bracket syntax.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 9 11:37:19 PST 2018


Author: aaronballman
Date: Fri Nov  9 11:37:18 2018
New Revision: 346547

URL: http://llvm.org/viewvc/llvm-project?rev=346547&view=rev
Log:
Allow a double-underscore spelling of Clang attributes using double square bracket syntax.

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.

Modified:
    cfe/trunk/lib/Sema/ParsedAttr.cpp
    cfe/trunk/test/SemaCXX/attr-optnone.cpp
    cfe/trunk/test/SemaCXX/switch-implicit-fallthrough.cpp

Modified: cfe/trunk/lib/Sema/ParsedAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/ParsedAttr.cpp?rev=346547&r1=346546&r2=346547&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/ParsedAttr.cpp (original)
+++ cfe/trunk/lib/Sema/ParsedAttr.cpp Fri Nov  9 11:37:18 2018
@@ -122,11 +122,12 @@ static StringRef normalizeAttrName(Strin
                                    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);
 

Modified: cfe/trunk/test/SemaCXX/attr-optnone.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-optnone.cpp?rev=346547&r1=346546&r2=346547&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-optnone.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-optnone.cpp Fri Nov  9 11:37:18 2018
@@ -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}}

Modified: cfe/trunk/test/SemaCXX/switch-implicit-fallthrough.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/switch-implicit-fallthrough.cpp?rev=346547&r1=346546&r2=346547&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/switch-implicit-fallthrough.cpp (original)
+++ cfe/trunk/test/SemaCXX/switch-implicit-fallthrough.cpp Fri Nov  9 11:37:18 2018
@@ -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;
+}




More information about the cfe-commits mailing list