[cfe-commits] r116906 - in /cfe/trunk: clang.xcodeproj/project.pbxproj docs/LanguageExtensions.html include/clang/CMakeLists.txt include/clang/Lex/CMakeLists.txt include/clang/Lex/Makefile include/clang/Lex/Preprocessor.h include/clang/Makefile lib/Lex/CMakeLists.txt lib/Lex/PPMacroExpansion.cpp test/Lexer/has_attribute.cpp

Anders Carlsson andersca at mac.com
Tue Oct 19 19:31:44 PDT 2010


Author: andersca
Date: Tue Oct 19 21:31:43 2010
New Revision: 116906

URL: http://llvm.org/viewvc/llvm-project?rev=116906&view=rev
Log:
Add a __has_attribute macro that works much like __has_feature and __has_builtin.

Added:
    cfe/trunk/include/clang/Lex/CMakeLists.txt
    cfe/trunk/include/clang/Lex/Makefile
    cfe/trunk/test/Lexer/has_attribute.cpp
Modified:
    cfe/trunk/clang.xcodeproj/project.pbxproj
    cfe/trunk/docs/LanguageExtensions.html
    cfe/trunk/include/clang/CMakeLists.txt
    cfe/trunk/include/clang/Lex/Preprocessor.h
    cfe/trunk/include/clang/Makefile
    cfe/trunk/lib/Lex/CMakeLists.txt
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp

Modified: cfe/trunk/clang.xcodeproj/project.pbxproj
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/clang.xcodeproj/project.pbxproj?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/clang.xcodeproj/project.pbxproj (original)
+++ cfe/trunk/clang.xcodeproj/project.pbxproj Tue Oct 19 21:31:43 2010
@@ -2039,6 +2039,7 @@
 			isa = PBXProject;
 			buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
 			compatibilityVersion = "Xcode 2.4";
+			developmentRegion = English;
 			hasScannedForEncodings = 1;
 			knownRegions = (
 				English,

Modified: cfe/trunk/docs/LanguageExtensions.html
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/LanguageExtensions.html?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/docs/LanguageExtensions.html (original)
+++ cfe/trunk/docs/LanguageExtensions.html Tue Oct 19 21:31:43 2010
@@ -136,6 +136,30 @@
 <p>The feature tag is described along with the language feature below.</p>
 
 <!-- ======================================================================= -->
+<h3 id="__has_attribute">__has_attribute</h3>
+<!-- ======================================================================= -->
+
+<p>This function-like macro takes a single identifier argument that is the name
+of an attribute.  It evaluates to 1 if the attribute is supported or 0 if not.  It
+can be used like this:</p>
+
+<blockquote>
+<pre>
+#ifndef __has_attribute         // Optional of course.
+  #define __has_attribute(x) 0  // Compatibility with non-clang compilers.
+#endif
+
+...
+#if __has_attribute(override) || \
+#define OVERRIDE __attribute__((override))
+#else
+#define OVERRIDE
+#endif
+...
+</pre>
+</blockquote>
+
+<!-- ======================================================================= -->
 <h2 id="has_include">Include File Checking Macros</h2>
 <!-- ======================================================================= -->
 

Modified: cfe/trunk/include/clang/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/CMakeLists.txt?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/include/clang/CMakeLists.txt (original)
+++ cfe/trunk/include/clang/CMakeLists.txt Tue Oct 19 21:31:43 2010
@@ -1,4 +1,5 @@
 add_subdirectory(AST)
 add_subdirectory(Basic)
 add_subdirectory(Driver)
+add_subdirectory(Lex)
 add_subdirectory(Serialization)

Added: cfe/trunk/include/clang/Lex/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/CMakeLists.txt?rev=116906&view=auto
==============================================================================
--- cfe/trunk/include/clang/Lex/CMakeLists.txt (added)
+++ cfe/trunk/include/clang/Lex/CMakeLists.txt Tue Oct 19 21:31:43 2010
@@ -0,0 +1,6 @@
+set(LLVM_TARGET_DEFINITIONS ../Basic/Attr.td)
+tablegen(AttrSpellings.inc
+         -gen-clang-attr-spelling-list
+         -I ${CMAKE_CURRENT_SOURCE_DIR}/../../)
+add_custom_target(ClangAttrSpellings
+  DEPENDS AttrSpellings.inc)

Added: cfe/trunk/include/clang/Lex/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Makefile?rev=116906&view=auto
==============================================================================
--- cfe/trunk/include/clang/Lex/Makefile (added)
+++ cfe/trunk/include/clang/Lex/Makefile Tue Oct 19 21:31:43 2010
@@ -0,0 +1,13 @@
+CLANG_LEVEL := ../../..
+TD_SRC_DIR = $(PROJ_SRC_DIR)/../Basic
+BUILT_SOURCES = AttrSpellings.inc 
+
+TABLEGEN_INC_FILES_COMMON = 1
+
+include $(CLANG_LEVEL)/Makefile
+
+$(ObjDir)/AttrSpellings.inc.tmp : $(TD_SRC_DIR)/Attr.td $(TBLGEN) \
+                                  $(ObjDir)/.dir
+	$(Echo) "Building Clang attribute spellings with tblgen"
+	$(Verb) $(TableGen) -gen-clang-attr-spelling-list -o $(call SYSPATH, $@) \
+		-I $(PROJ_SRC_DIR)/../../ $<

Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Oct 19 21:31:43 2010
@@ -83,6 +83,7 @@
   IdentifierInfo *Ident__VA_ARGS__;                // __VA_ARGS__
   IdentifierInfo *Ident__has_feature;              // __has_feature
   IdentifierInfo *Ident__has_builtin;              // __has_builtin
+  IdentifierInfo *Ident__has_attribute;            // __has_attribute
   IdentifierInfo *Ident__has_include;              // __has_include
   IdentifierInfo *Ident__has_include_next;         // __has_include_next
 

Modified: cfe/trunk/include/clang/Makefile
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Makefile?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/include/clang/Makefile (original)
+++ cfe/trunk/include/clang/Makefile Tue Oct 19 21:31:43 2010
@@ -1,5 +1,5 @@
 CLANG_LEVEL := ../..
-DIRS := AST Basic Driver Serialization
+DIRS := AST Basic Driver Lex Serialization
 
 include $(CLANG_LEVEL)/Makefile
 

Modified: cfe/trunk/lib/Lex/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/CMakeLists.txt?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/CMakeLists.txt (original)
+++ cfe/trunk/lib/Lex/CMakeLists.txt Tue Oct 19 21:31:43 2010
@@ -26,4 +26,4 @@
   TokenLexer.cpp
   )
 
-add_dependencies(clangLex ClangDiagnosticLex)
+add_dependencies(clangLex ClangDiagnosticLex ClangAttrSpellings)

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=116906&r1=116905&r2=116906&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Tue Oct 19 21:31:43 2010
@@ -70,6 +70,7 @@
   // Clang Extensions.
   Ident__has_feature      = RegisterBuiltinMacro(*this, "__has_feature");
   Ident__has_builtin      = RegisterBuiltinMacro(*this, "__has_builtin");
+  Ident__has_attribute    = RegisterBuiltinMacro(*this, "__has_attribute");
   Ident__has_include      = RegisterBuiltinMacro(*this, "__has_include");
   Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");
 
@@ -535,6 +536,14 @@
            .Default(false);
 }
 
+/// 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())
+#include "clang/Lex/AttrSpellings.inc"
+        .Default(false);
+}
+
 /// EvaluateHasIncludeCommon - Process a '__has_include("path")'
 /// or '__has_include_next("path")' expression.
 /// Returns true if successful.
@@ -767,7 +776,8 @@
     OS << CounterValue++;
     Tok.setKind(tok::numeric_constant);
   } else if (II == Ident__has_feature ||
-             II == Ident__has_builtin) {
+             II == Ident__has_builtin ||
+             II == Ident__has_attribute) {
     // The argument to these two builtins should be a parenthesized identifier.
     SourceLocation StartLoc = Tok.getLocation();
 
@@ -795,7 +805,9 @@
     else if (II == Ident__has_builtin) {
       // Check for a builtin is trivial.
       Value = FeatureII->getBuiltinID() != 0;
-    } else {
+    } else if (II == Ident__has_attribute)
+      Value = HasAttribute(FeatureII);
+    else {
       assert(II == Ident__has_feature && "Must be feature check");
       Value = HasFeature(*this, FeatureII);
     }

Added: cfe/trunk/test/Lexer/has_attribute.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/has_attribute.cpp?rev=116906&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/has_attribute.cpp (added)
+++ cfe/trunk/test/Lexer/has_attribute.cpp Tue Oct 19 21:31:43 2010
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -E %s -o - | FileCheck %s
+
+// CHECK: always_inline
+#if __has_attribute(always_inline)
+int always_inline();
+#endif
+
+// CHECK: no_dummy_attribute
+#if !__has_attribute(dummy_attribute)
+int no_dummy_attribute();
+#endif
+





More information about the cfe-commits mailing list