[cfe-commits] r151445 - in /cfe/trunk: docs/LanguageExtensions.html lib/Lex/PPMacroExpansion.cpp lib/Sema/AttributeList.cpp test/Lexer/has_extension.c test/Preprocessor/feature_tests.c

Richard Smith richard-llvm at metafoo.co.uk
Sat Feb 25 02:41:11 PST 2012


Author: rsmith
Date: Sat Feb 25 04:41:10 2012
New Revision: 151445

URL: http://llvm.org/viewvc/llvm-project?rev=151445&view=rev
Log:
Accept __has_feature(__feature__) as a synonym for __has_feature(feature) (and
likewise for __has_extension). Patch by Jonathan Sauer!

Modified:
    cfe/trunk/docs/LanguageExtensions.html
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Sema/AttributeList.cpp
    cfe/trunk/test/Lexer/has_extension.c
    cfe/trunk/test/Preprocessor/feature_tests.c

Modified: cfe/trunk/docs/LanguageExtensions.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LanguageExtensions.html?rev=151445&r1=151444&r2=151445&view=diff
==============================================================================
--- cfe/trunk/docs/LanguageExtensions.html (original)
+++ cfe/trunk/docs/LanguageExtensions.html Sat Feb 25 04:41:10 2012
@@ -228,6 +228,11 @@
 
 <p>The feature tag is described along with the language feature below.</p>
 
+<p>The feature name or extension name can also be specified with a preceding and
+following <code>__</code> (double underscore) to avoid interference from a macro
+with the same name. For instance, <code>__always_inline__</code> can be used
+instead of <code>always_inline</code>.</p>
+
 <!-- ======================================================================= -->
 <h3><a name="__has_attribute">__has_attribute</a></h3>
 <!-- ======================================================================= -->

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=151445&r1=151444&r2=151445&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Sat Feb 25 04:41:10 2012
@@ -589,8 +589,13 @@
 /// specified by the identifier as a standard language feature.
 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
   const LangOptions &LangOpts = PP.getLangOptions();
+  StringRef Feature = II->getName();
 
-  return llvm::StringSwitch<bool>(II->getName())
+  // Normalize the feature name, __foo__ becomes foo.
+  if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4)
+    Feature = Feature.substr(2, Feature.size() - 4);
+
+  return llvm::StringSwitch<bool>(Feature)
            .Case("address_sanitizer", LangOpts.AddressSanitizer)
            .Case("attribute_analyzer_noreturn", true)
            .Case("attribute_availability", true)
@@ -724,10 +729,16 @@
     return false;
 
   const LangOptions &LangOpts = PP.getLangOptions();
+  StringRef Extension = II->getName();
+
+  // Normalize the extension name, __foo__ becomes foo.
+  if (Extension.startswith("__") && Extension.endswith("__") &&
+      Extension.size() >= 4)
+    Extension = Extension.substr(2, Extension.size() - 4);
 
   // Because we inherit the feature list from HasFeature, this string switch
   // must be less restrictive than HasFeature's.
-  return llvm::StringSwitch<bool>(II->getName())
+  return llvm::StringSwitch<bool>(Extension)
            // C11 features supported by other languages as extensions.
            .Case("c_alignas", true)
            .Case("c_atomic", true)

Modified: cfe/trunk/lib/Sema/AttributeList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AttributeList.cpp?rev=151445&r1=151444&r2=151445&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AttributeList.cpp (original)
+++ cfe/trunk/lib/Sema/AttributeList.cpp Sat Feb 25 04:41:10 2012
@@ -101,7 +101,8 @@
   StringRef AttrName = Name->getName();
 
   // Normalize the attribute name, __foo__ becomes foo.
-  if (AttrName.startswith("__") && AttrName.endswith("__"))
+  if (AttrName.startswith("__") && AttrName.endswith("__") &&
+      AttrName.size() >= 4)
     AttrName = AttrName.substr(2, AttrName.size() - 4);
 
   return llvm::StringSwitch<AttributeList::Kind>(AttrName)

Modified: cfe/trunk/test/Lexer/has_extension.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/has_extension.c?rev=151445&r1=151444&r2=151445&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/has_extension.c (original)
+++ cfe/trunk/test/Lexer/has_extension.c Sat Feb 25 04:41:10 2012
@@ -36,3 +36,9 @@
 int no_c_alignas();
 #endif
 
+// Arbitrary feature to test that the extension name can be surrounded with
+// double underscores.
+// CHECK-PED-NONE: has_double_underscores
+#if __has_extension(__c_alignas__)
+int has_double_underscores();
+#endif

Modified: cfe/trunk/test/Preprocessor/feature_tests.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/feature_tests.c?rev=151445&r1=151444&r2=151445&view=diff
==============================================================================
--- cfe/trunk/test/Preprocessor/feature_tests.c (original)
+++ cfe/trunk/test/Preprocessor/feature_tests.c Sat Feb 25 04:41:10 2012
@@ -21,7 +21,9 @@
 #error Clang should not have this
 #endif
 
-
+#if !__has_feature(__attribute_deprecated_with_message__)
+#error Feature name in double underscores does not work
+#endif
 
 // Make sure we have x86 builtins only (forced with target triple).
 





More information about the cfe-commits mailing list