[clang] f4be5ed - [clang][pp] only __is_target_environment(unknown) should match unknown target triple environment

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 2 13:49:14 PDT 2022


Author: Alex Lorenz
Date: 2022-11-02T13:49:04-07:00
New Revision: f4be5ed6a3fef0b2b0c60b29e1c0638926638d28

URL: https://github.com/llvm/llvm-project/commit/f4be5ed6a3fef0b2b0c60b29e1c0638926638d28
DIFF: https://github.com/llvm/llvm-project/commit/f4be5ed6a3fef0b2b0c60b29e1c0638926638d28.diff

LOG: [clang][pp] only __is_target_environment(unknown) should match unknown target triple environment

Added: 
    clang/test/Preprocessor/is_target_unknown_environment.c

Modified: 
    clang/lib/Lex/PPMacroExpansion.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index e79d76cbf3857..ffc758d47c7a2 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1447,6 +1447,11 @@ static bool isTargetEnvironment(const TargetInfo &TI,
                                 const IdentifierInfo *II) {
   std::string EnvName = (llvm::Twine("---") + II->getName().lower()).str();
   llvm::Triple Env(EnvName);
+  // The unknown environment is matched only if
+  // '__is_target_environment(unknown)' is used.
+  if (Env.getEnvironment() == llvm::Triple::UnknownEnvironment &&
+      EnvName != "---unknown")
+    return false;
   return TI.getTriple().getEnvironment() == Env.getEnvironment();
 }
 

diff  --git a/clang/test/Preprocessor/is_target_unknown_environment.c b/clang/test/Preprocessor/is_target_unknown_environment.c
new file mode 100644
index 0000000000000..9462ef442fecd
--- /dev/null
+++ b/clang/test/Preprocessor/is_target_unknown_environment.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-macos12 -verify %s
+
+// expected-no-diagnostics
+
+#if !__is_target_environment(unknown)
+#error "mismatching environment"
+#endif
+
+#if __is_target_environment(simulator) || __is_target_environment(SIMULATOR)
+#error "mismatching environment"
+#endif
+
+#if __is_target_environment(invalidEnv)
+#error "invalid environment must not be matched"
+#endif


        


More information about the cfe-commits mailing list