Implement double underscore names support in __has_attribute

llvm-svn: 151809
This commit is contained in:
Jean-Daniel Dupas 2012-03-01 14:53:16 +00:00
parent 8e9d772c5a
commit 908f130d58
3 changed files with 25 additions and 1 deletions

View 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>
<!-- ======================================================================= -->

View 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);
}

View File

@ -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