<div dir="ltr">Sorry, I forgot to add the tests in SVN. I committed them in <span style="font-size:12.800000190734863px">320735</span></div><div class="gmail_extra"><br><div class="gmail_quote">On 14 December 2017 at 11:22, Alex Lorenz via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: arphaman<br>
Date: Thu Dec 14 11:22:02 2017<br>
New Revision: 320734<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=320734&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=320734&view=rev</a><br>
Log:<br>
[Preprocessor] Implement __is_target_{arch|vendor|os|<wbr>environment} function-like<br>
builtin macros<br>
<br>
This patch implements the __is_target_arch, __is_target_vendor, __is_target_os,<br>
and __is_target_environment Clang preprocessor extensions that were proposed by<br>
@compnerd in Bob's cfe-dev post:<br>
<a href="http://lists.llvm.org/pipermail/cfe-dev/2017-November/056166.html" rel="noreferrer" target="_blank">http://lists.llvm.org/<wbr>pipermail/cfe-dev/2017-<wbr>November/056166.html</a>.<br>
<br>
These macros can be used to examine the components of the target triple at<br>
compile time. A has_builtin(is_target_???) preprocessor check can be used to<br>
check for their availability.<br>
<br>
__is_target_arch allows you to check if an arch is specified without worring<br>
about a specific subarch, e.g.<br>
<br>
__is_target_arch(arm) returns 1 for the target arch "armv7"<br>
__is_target_arch(armv7) returns 1 for the target arch "armv7"<br>
__is_target_arch(armv6) returns 0 for the target arch "armv7"<br>
<br>
__is_target_vendor and __is_target_environment match the specific vendor<br>
or environment. __is_target_os matches the specific OS, but<br>
__is_target_os(darwin) will match any Darwin-based OS. "Unknown" can be used<br>
to test if the triple's component is specified.<br>
<br>
rdar://35753116<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D41087" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D41087</a><br>
<br>
Modified:<br>
    cfe/trunk/docs/ReleaseNotes.<wbr>rst<br>
    cfe/trunk/include/clang/Lex/<wbr>Preprocessor.h<br>
    cfe/trunk/lib/Lex/<wbr>PPMacroExpansion.cpp<br>
<br>
Modified: cfe/trunk/docs/ReleaseNotes.<wbr>rst<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/ReleaseNotes.rst?rev=320734&r1=320733&r2=320734&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/docs/<wbr>ReleaseNotes.rst?rev=320734&<wbr>r1=320733&r2=320734&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/docs/ReleaseNotes.<wbr>rst (original)<br>
+++ cfe/trunk/docs/ReleaseNotes.<wbr>rst Thu Dec 14 11:22:02 2017<br>
@@ -110,6 +110,10 @@ Non-comprehensive list of changes in thi<br>
   If a gcc installation is found, it still prefers ``.ctors`` if the found<br>
   gcc is older than 4.7.0.<br>
<br>
+- The new builtin preprocessor macros ``__is_target_arch``,<br>
+  ``__is_target_vendor``, ``__is_target_os``, and ``__is_target_environment``<br>
+  can be used to to examine the individual components of the target triple.<br>
+<br>
 New Compiler Flags<br>
 ------------------<br>
<br>
<br>
Modified: cfe/trunk/include/clang/Lex/<wbr>Preprocessor.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=320734&r1=320733&r2=320734&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/include/<wbr>clang/Lex/Preprocessor.h?rev=<wbr>320734&r1=320733&r2=320734&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/include/clang/Lex/<wbr>Preprocessor.h (original)<br>
+++ cfe/trunk/include/clang/Lex/<wbr>Preprocessor.h Thu Dec 14 11:22:02 2017<br>
@@ -175,6 +175,10 @@ class Preprocessor {<br>
   IdentifierInfo *Ident__has_cpp_attribute;        // __has_cpp_attribute<br>
   IdentifierInfo *Ident__has_c_attribute;          // __has_c_attribute<br>
   IdentifierInfo *Ident__has_declspec;             // __has_declspec_attribute<br>
+  IdentifierInfo *Ident__is_target_arch;           // __is_target_arch<br>
+  IdentifierInfo *Ident__is_target_vendor;         // __is_target_vendor<br>
+  IdentifierInfo *Ident__is_target_os;             // __is_target_os<br>
+  IdentifierInfo *Ident__is_target_environment;    // __is_target_environment<br>
<br>
   SourceLocation DATELoc, TIMELoc;<br>
<br>
<br>
Modified: cfe/trunk/lib/Lex/<wbr>PPMacroExpansion.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=320734&r1=320733&r2=320734&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/Lex/<wbr>PPMacroExpansion.cpp?rev=<wbr>320734&r1=320733&r2=320734&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/Lex/<wbr>PPMacroExpansion.cpp (original)<br>
+++ cfe/trunk/lib/Lex/<wbr>PPMacroExpansion.cpp Thu Dec 14 11:22:02 2017<br>
@@ -375,6 +375,11 @@ void Preprocessor::<wbr>RegisterBuiltinMacros<br>
   Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next");<br>
   Ident__has_warning      = RegisterBuiltinMacro(*this, "__has_warning");<br>
   Ident__is_identifier    = RegisterBuiltinMacro(*this, "__is_identifier");<br>
+  Ident__is_target_arch   = RegisterBuiltinMacro(*this, "__is_target_arch");<br>
+  Ident__is_target_vendor = RegisterBuiltinMacro(*this, "__is_target_vendor");<br>
+  Ident__is_target_os     = RegisterBuiltinMacro(*this, "__is_target_os");<br>
+  Ident__is_target_environment =<br>
+      RegisterBuiltinMacro(*this, "__is_target_environment");<br>
<br>
   // Modules.<br>
   Ident__building_module  = RegisterBuiltinMacro(*this, "__building_module");<br>
@@ -1593,6 +1598,57 @@ static IdentifierInfo *ExpectFeatureIden<br>
   return nullptr;<br>
 }<br>
<br>
+/// Implements the __is_target_arch builtin macro.<br>
+static bool isTargetArch(const TargetInfo &TI, const IdentifierInfo *II) {<br>
+  std::string ArchName = II->getName().lower() + "--";<br>
+  llvm::Triple Arch(ArchName);<br>
+  const llvm::Triple &TT = TI.getTriple();<br>
+  if (TT.isThumb()) {<br>
+    // arm matches thumb or thumbv7. armv7 matches thumbv7.<br>
+    if ((Arch.getSubArch() == llvm::Triple::NoSubArch ||<br>
+         Arch.getSubArch() == TT.getSubArch()) &&<br>
+        ((TT.getArch() == llvm::Triple::thumb &&<br>
+          Arch.getArch() == llvm::Triple::arm) ||<br>
+         (TT.getArch() == llvm::Triple::thumbeb &&<br>
+          Arch.getArch() == llvm::Triple::armeb)))<br>
+      return true;<br>
+  }<br>
+  // Check the parsed arch when it has no sub arch to allow Clang to<br>
+  // match thumb to thumbv7 but to prohibit matching thumbv6 to thumbv7.<br>
+  return (Arch.getSubArch() == llvm::Triple::NoSubArch &&<br>
+          Arch.getArch() == TT.getArch()) ||<br>
+         Arch.getArchName() == TT.getArchName();<br>
+}<br>
+<br>
+/// Implements the __is_target_vendor builtin macro.<br>
+static bool isTargetVendor(const TargetInfo &TI, const IdentifierInfo *II) {<br>
+  StringRef VendorName = TI.getTriple().getVendorName()<wbr>;<br>
+  if (VendorName.empty())<br>
+    VendorName = "unknown";<br>
+  return VendorName.equals_lower(II-><wbr>getName());<br>
+}<br>
+<br>
+/// Implements the __is_target_os builtin macro.<br>
+static bool isTargetOS(const TargetInfo &TI, const IdentifierInfo *II) {<br>
+  std::string OSName =<br>
+      (llvm::Twine("unknown-unknown-<wbr>") + II->getName().lower()).str();<br>
+  llvm::Triple OS(OSName);<br>
+  if (OS.getOS() == llvm::Triple::Darwin) {<br>
+    // Darwin matches macos, ios, etc.<br>
+    return TI.getTriple().isOSDarwin();<br>
+  }<br>
+  return TI.getTriple().getOS() == OS.getOS();<br>
+}<br>
+<br>
+/// Implements the __is_target_environment builtin macro.<br>
+static bool isTargetEnvironment(const TargetInfo &TI,<br>
+                                const IdentifierInfo *II) {<br>
+  StringRef EnvName = TI.getTriple().<wbr>getEnvironmentName();<br>
+  if (EnvName.empty())<br>
+    EnvName = "unknown";<br>
+  return EnvName.equals_lower(II-><wbr>getName());<br>
+}<br>
+<br>
 /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded<br>
 /// as a builtin macro, handle it and return the next token as 'Tok'.<br>
 void Preprocessor::<wbr>ExpandBuiltinMacro(Token &Tok) {<br>
@@ -1755,6 +1811,10 @@ void Preprocessor::<wbr>ExpandBuiltinMacro(To<br>
                       .Case("__make_integer_seq", LangOpts.CPlusPlus)<br>
                       .Case("__type_pack_element", LangOpts.CPlusPlus)<br>
                       .Case("__builtin_available", true)<br>
+                      .Case("__is_target_arch", true)<br>
+                      .Case("__is_target_vendor", true)<br>
+                      .Case("__is_target_os", true)<br>
+                      .Case("__is_target_<wbr>environment", true)<br>
                       .Default(false);<br>
         }<br>
       });<br>
@@ -1906,6 +1966,34 @@ void Preprocessor::<wbr>ExpandBuiltinMacro(To<br>
       Diag(LParenLoc, diag::note_matching) << tok::l_paren;<br>
     }<br>
     return;<br>
+  } else if (II == Ident__is_target_arch) {<br>
+    EvaluateFeatureLikeBuiltinMacr<wbr>o(<br>
+        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {<br>
+          IdentifierInfo *II = ExpectFeatureIdentifierInfo(<br>
+              Tok, *this, diag::err_feature_check_<wbr>malformed);<br>
+          return II && isTargetArch(getTargetInfo(), II);<br>
+        });<br>
+  } else if (II == Ident__is_target_vendor) {<br>
+    EvaluateFeatureLikeBuiltinMacr<wbr>o(<br>
+        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {<br>
+          IdentifierInfo *II = ExpectFeatureIdentifierInfo(<br>
+              Tok, *this, diag::err_feature_check_<wbr>malformed);<br>
+          return II && isTargetVendor(getTargetInfo()<wbr>, II);<br>
+        });<br>
+  } else if (II == Ident__is_target_os) {<br>
+    EvaluateFeatureLikeBuiltinMacr<wbr>o(<br>
+        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {<br>
+          IdentifierInfo *II = ExpectFeatureIdentifierInfo(<br>
+              Tok, *this, diag::err_feature_check_<wbr>malformed);<br>
+          return II && isTargetOS(getTargetInfo(), II);<br>
+        });<br>
+  } else if (II == Ident__is_target_environment) {<br>
+    EvaluateFeatureLikeBuiltinMacr<wbr>o(<br>
+        OS, Tok, II, *this, [this](Token &Tok, bool &HasLexedNextToken) -> int {<br>
+          IdentifierInfo *II = ExpectFeatureIdentifierInfo(<br>
+              Tok, *this, diag::err_feature_check_<wbr>malformed);<br>
+          return II && isTargetEnvironment(<wbr>getTargetInfo(), II);<br>
+        });<br>
   } else {<br>
     llvm_unreachable("Unknown identifier!");<br>
   }<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>