[PATCH] D102322: [clang-tidy] Add '-target' CLI option to override target triple

Georgy Komarov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 12 03:39:09 PDT 2021


jubnzv created this revision.
jubnzv added a project: clang-tools-extra.
Herald added subscribers: kbarton, xazax.hun, nemanjai.
jubnzv requested review of this revision.

The added option allows the user to specify the target for which //clang-tidy// checks will be executed. This is necessary for users who run the tool on project that compiles for a different target platform.

This option also makes it easier to test of //clang-tidy// and allows to run platform-specific tests on any host system.
This will fix the failed tests when they are run on a Windows host, but the toolchain is targeted a non-Windows platform. The problem is described here: https://reviews.llvm.org/D101259#2739466.


https://reviews.llvm.org/D102322

Files:
  clang-tools-extra/clang-tidy/ClangTidy.cpp
  clang-tools-extra/clang-tidy/ClangTidy.h
  clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
  clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-pro-type-vararg-ms.cpp
@@ -2,9 +2,7 @@
 // Ensure that the 'cppcoreguidelines-pro-type-vararg' check works with the
 // built-in va_list on Windows systems.
 
-// REQUIRES: system-windows
-
-// RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-vararg %t
+// RUN: %check_clang_tidy -target="x86_64-windows" %s cppcoreguidelines-pro-type-vararg %t
 
 void test_ms_va_list(int a, ...) {
   __builtin_ms_va_list ap;
Index: clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
===================================================================
--- clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
+++ clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
@@ -256,6 +256,12 @@
 )"),
                               cl::init(false), cl::cat(ClangTidyCategory));
 
+static cl::opt<std::string> TargetTriple("target", cl::desc(R"(
+Override target triple for clang-tidy. If not set, the host target will be
+used.)"),
+                                         cl::init(""),
+                                         cl::cat(ClangTidyCategory));
+
 namespace clang {
 namespace tidy {
 
@@ -488,11 +494,20 @@
   llvm::InitializeAllTargetMCs();
   llvm::InitializeAllAsmParsers();
 
+  if (!TargetTriple.empty()) {
+    TargetTriple = Triple::normalize(TargetTriple);
+    auto T = Triple(TargetTriple);
+    if (T.getArch() == Triple::UnknownArch) {
+      llvm::errs() << "Error: unknown target triple '" << TargetTriple << "'\n";
+      return 1;
+    }
+  }
+
   ClangTidyContext Context(std::move(OwningOptionsProvider),
                            AllowEnablingAnalyzerAlphaCheckers);
   std::vector<ClangTidyError> Errors =
       runClangTidy(Context, OptionsParser->getCompilations(), PathList, BaseFS,
-                   FixNotes, EnableCheckProfile, ProfilePrefix);
+                   FixNotes, EnableCheckProfile, TargetTriple, ProfilePrefix);
   bool FoundErrors = llvm::find_if(Errors, [](const ClangTidyError &E) {
                        return E.DiagLevel == ClangTidyError::Error;
                      }) != Errors.end();
Index: clang-tools-extra/clang-tidy/ClangTidy.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.h
+++ clang-tools-extra/clang-tidy/ClangTidy.h
@@ -80,6 +80,7 @@
              ArrayRef<std::string> InputFiles,
              llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
              bool ApplyAnyFix, bool EnableCheckProfile = false,
+             const std::string &TargetTriple = "",
              llvm::StringRef StoreCheckProfile = StringRef());
 
 /// Controls what kind of fixes clang-tidy is allowed to apply.
Index: clang-tools-extra/clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidy.cpp
+++ clang-tools-extra/clang-tidy/ClangTidy.cpp
@@ -515,15 +515,18 @@
              ArrayRef<std::string> InputFiles,
              llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> BaseFS,
              bool ApplyAnyFix, bool EnableCheckProfile,
+             const std::string &TargetTriple,
              llvm::StringRef StoreCheckProfile) {
   ClangTool Tool(Compilations, InputFiles,
                  std::make_shared<PCHContainerOperations>(), BaseFS);
 
   // Add extra arguments passed by the clang-tidy command-line.
   ArgumentsAdjuster PerFileExtraArgumentsInserter =
-      [&Context](const CommandLineArguments &Args, StringRef Filename) {
+      [&Context, &TargetTriple](const CommandLineArguments &Args, StringRef Filename) {
         ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
         CommandLineArguments AdjustedArgs = Args;
+        if (!TargetTriple.empty())
+          AdjustedArgs.push_back("--target=" + TargetTriple);
         if (Opts.ExtraArgsBefore) {
           auto I = AdjustedArgs.begin();
           if (I != AdjustedArgs.end() && !StringRef(*I).startswith("-"))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102322.344752.patch
Type: text/x-patch
Size: 4229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210512/f1849c7b/attachment.bin>


More information about the cfe-commits mailing list