[Driver] Adjust -fsanitize=function & -mexecute-only interop after D158614
authorFangrui Song <i@maskray.me>
Wed, 30 Aug 2023 19:29:50 +0000 (19:29 +0000)
committerTobias Hieta <tobias@hieta.se>
Fri, 1 Sep 2023 06:26:36 +0000 (08:26 +0200)
clangDriver depends on clangBasic, so clangBasic should not depend on
clangDriver, even just its header. Also remove clangBasic's dependency
on LLVMOption.

The issue can be seen through the bazel commit
d26dd681f9726ed7d43d7c0bdd8ee3cb2db69a2b which is reverted now.

Add hasFlagNoClaim and use it as we don't want to suppress
-Wunused-command-line-argument for -mexecute-only just because
-fsanitize= is specified.

clang/include/clang/Basic/Sanitizers.h
clang/lib/Basic/CMakeLists.txt
clang/lib/Basic/Sanitizers.cpp
clang/lib/Driver/SanitizerArgs.cpp
llvm/include/llvm/Option/ArgList.h
llvm/lib/Option/ArgList.cpp

index c212f80..4659e45 100644 (file)
@@ -209,11 +209,6 @@ StringRef AsanDetectStackUseAfterReturnModeToString(
 llvm::AsanDetectStackUseAfterReturnMode
 AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr);
 
-/// Return true if an execute-only target disallows data access to code
-/// sections.
-bool isExecuteOnlyTarget(const llvm::Triple &Triple,
-                         const llvm::opt::ArgList &Args);
-
 } // namespace clang
 
 #endif // LLVM_CLANG_BASIC_SANITIZERS_H
index d6620ec..caa1b60 100644 (file)
@@ -1,5 +1,4 @@
 set(LLVM_LINK_COMPONENTS
-  Option
   Support
   TargetParser
   )
index 6fbc32d..62ccdf8 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "clang/Basic/Sanitizers.h"
-#include "clang/Driver/Options.h"
 #include "llvm/ADT/Hashing.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringSwitch.h"
-#include "llvm/Option/ArgList.h"
 #include "llvm/Support/MathExtras.h"
-#include "llvm/TargetParser/Triple.h"
 
 using namespace clang;
 
@@ -115,14 +112,4 @@ AsanDetectStackUseAfterReturnModeFromString(StringRef modeStr) {
       .Default(llvm::AsanDetectStackUseAfterReturnMode::Invalid);
 }
 
-bool isExecuteOnlyTarget(const llvm::Triple &Triple,
-                         const llvm::opt::ArgList &Args) {
-  if (Triple.isPS5())
-    return true;
-
-  // On Arm, the clang `-mexecute-only` option is used to generate the
-  // execute-only output (no data access to code sections).
-  return Args.hasFlag(clang::driver::options::OPT_mexecute_only,
-                      clang::driver::options::OPT_mno_execute_only, false);
-}
 } // namespace clang
index a4e9475..12fe55b 100644 (file)
@@ -143,6 +143,16 @@ static std::string describeSanitizeArg(const llvm::opt::Arg *A,
 /// Sanitizers set.
 static std::string toString(const clang::SanitizerSet &Sanitizers);
 
+/// Return true if an execute-only target disallows data access to code
+/// sections.
+static bool isExecuteOnlyTarget(const llvm::Triple &Triple,
+                                const llvm::opt::ArgList &Args) {
+  if (Triple.isPS5())
+    return true;
+  return Args.hasFlagNoClaim(options::OPT_mexecute_only,
+                             options::OPT_mno_execute_only, false);
+}
+
 static void validateSpecialCaseListFormat(const Driver &D,
                                           std::vector<std::string> &SCLFiles,
                                           unsigned MalformedSCLErrorDiagID,
index 310c890..c57bd23 100644 (file)
@@ -299,6 +299,7 @@ public:
   /// \p Default if neither option is given. If both the option and its
   /// negation are present, the last one wins.
   bool hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const;
+  bool hasFlagNoClaim(OptSpecifier Pos, OptSpecifier Neg, bool Default) const;
 
   /// hasFlag - Given an option \p Pos, an alias \p PosAlias and its negative
   /// form \p Neg, return true if the option or its alias is present, false if
index 400beda..86f28e5 100644 (file)
@@ -75,6 +75,13 @@ bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier Neg, bool Default) const {
   return Default;
 }
 
+bool ArgList::hasFlagNoClaim(OptSpecifier Pos, OptSpecifier Neg,
+                             bool Default) const {
+  if (Arg *A = getLastArgNoClaim(Pos, Neg))
+    return A->getOption().matches(Pos);
+  return Default;
+}
+
 bool ArgList::hasFlag(OptSpecifier Pos, OptSpecifier PosAlias, OptSpecifier Neg,
                       bool Default) const {
   if (Arg *A = getLastArg(Pos, PosAlias, Neg))