]> granicus.if.org Git - clang/commitdiff
Implement double underscore names support in __has_attribute
authorJean-Daniel Dupas <devlists@shadowlab.org>
Thu, 1 Mar 2012 14:53:16 +0000 (14:53 +0000)
committerJean-Daniel Dupas <devlists@shadowlab.org>
Thu, 1 Mar 2012 14:53:16 +0000 (14:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151809 91177308-0d34-0410-b5e6-96231b3b80d8

docs/LanguageExtensions.html
lib/Lex/PPMacroExpansion.cpp
test/Preprocessor/has_attribute.c [new file with mode: 0644]

index be26d62414763eb100f3a3d32463aca9ce8b8188..23aa4fe9b920b8c383d12a0ee06c1888eed8d6dc 100644 (file)
@@ -257,6 +257,11 @@ can be used like this:</p>
 </pre>
 </blockquote>
 
+<p>The attribute 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>
+
 <!-- ======================================================================= -->
 <h2 id="has_include">Include File Checking Macros</h2>
 <!-- ======================================================================= -->
index 007be3bed2d67ff1f714462459f67d80e7e5d538..56ce407c1869a87e89a75d1c875f5c07128907e2 100644 (file)
@@ -760,7 +760,12 @@ static bool HasExtension(const Preprocessor &PP, const IdentifierInfo *II) {
 /// HasAttribute -  Return true if we recognize and implement the attribute
 /// specified by the given identifier.
 static bool HasAttribute(const IdentifierInfo *II) {
-    return llvm::StringSwitch<bool>(II->getName())
+  StringRef Name = II->getName();
+  // Normalize the attribute name, __foo__ becomes foo.
+  if (Name.startswith("__") && Name.endswith("__") && Name.size() >= 4)
+    Name = Name.substr(2, Name.size() - 4);
+
+  return llvm::StringSwitch<bool>(Name)
 #include "clang/Lex/AttrSpellings.inc"
         .Default(false);
 }
diff --git a/test/Preprocessor/has_attribute.c b/test/Preprocessor/has_attribute.c
new file mode 100644 (file)
index 0000000..825fa06
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 %s
+// RUN: %clang_cc1 %s -E
+#ifndef __has_attribute
+#error Should have __has_attribute
+#endif
+
+#if __has_attribute(something_we_dont_have)
+#error Bad
+#endif
+
+#if !__has_attribute(__always_inline__) || \
+    !__has_attribute(always_inline)
+#error Clang should have this
+#endif