Index: test/Lexer/has_extension.c
===================================================================
--- test/Lexer/has_extension.c	(revision 151367)
+++ test/Lexer/has_extension.c	(working copy)
@@ -36,3 +36,11 @@
 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) && !__has_extension(__c_alignas__)
+#else
+int has_double_underscores();
+#endif
+
Index: test/Preprocessor/feature_tests.c
===================================================================
--- test/Preprocessor/feature_tests.c	(revision 151367)
+++ test/Preprocessor/feature_tests.c	(working copy)
@@ -21,8 +21,11 @@
 #error Clang should not have this
 #endif
 
+#if __has_feature(attribute_deprecated_with_message) && \
+    !__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).
 
 #if !__has_builtin(__builtin_ia32_emms) || \
Index: docs/LanguageExtensions.html
===================================================================
--- docs/LanguageExtensions.html	(revision 151367)
+++ docs/LanguageExtensions.html	(working copy)
@@ -228,6 +228,12 @@
 
 <p>The feature tag is described along with the language feature below.</p>
 
+<p>The feature name as well as the extension name can also be specified with
+a preceding and following <code>__</code> (double underscore) to use it
+without being concerned about a macro with the same name. I.e.
+<code>__always_inline__</code> can be used instead of
+<code>always_inline</code>.</p>
+
 <!-- ======================================================================= -->
 <h3><a name="__has_attribute">__has_attribute</a></h3>
 <!-- ======================================================================= -->
Index: lib/Sema/AttributeList.cpp
===================================================================
--- lib/Sema/AttributeList.cpp	(revision 151367)
+++ lib/Sema/AttributeList.cpp	(working copy)
@@ -101,7 +101,7 @@
   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)
Index: lib/Lex/PPMacroExpansion.cpp
===================================================================
--- lib/Lex/PPMacroExpansion.cpp	(revision 151367)
+++ lib/Lex/PPMacroExpansion.cpp	(working copy)
@@ -589,8 +589,11 @@
 /// specified by the identifier as a standard language feature.
 static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
   const LangOptions &LangOpts = PP.getLangOptions();
-
-  return llvm::StringSwitch<bool>(II->getName())
+  StringRef          Feature = 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 +727,13 @@
     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)
