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