[PATCH] D102322: [clang-tidy] Add '-target' CLI option to override target triple
jubnzv abandoned this revision. jubnzv added a comment. Thank you, I didn't know that it is possible to set the target triple using `--extra-arg`. I created a new revision with this fix: https://reviews.llvm.org/D102337. For some reasons, the `--target=unknown-windows` doesn't work for me with the following error: `error: unknown target triple 'unknown-unknown-windows-msvc19.11.0', please use -triple or -arch [clang-diagnostic-error]`. But `--target=x86_64-windows` works fine. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D102322/new/ https://reviews.llvm.org/D102322 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D102322: [clang-tidy] Add '-target' CLI option to override target triple
njames93 added a comment. This change doesn't seem to accomplish anything. We can already set the target via the compile command. This can be done in the compilation database directly or by using the `extra-arg` and `extra-arg-before` command line arguments. If you're not using a compilation database, then you can pass the compile command to clang-tidy after an empty `--` argument. For the test case, this is sufficient to fix the issue: // RUN: %check_clang_tidy %s cppcoreguidelines-pro-type-vararg %t -- --extra-arg=--target=x86_64-windows CHANGES SINCE LAST ACTION https://reviews.llvm.org/D102322/new/ https://reviews.llvm.org/D102322 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D102322: [clang-tidy] Add '-target' CLI option to override target triple
jubnzv updated this revision to Diff 344758. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D102322/new/ 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 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 Errors = runClangTidy(Context, OptionsParser->getCompilations(), PathList, BaseFS, - FixNotes, EnableCheckProfile, ProfilePrefix); + FixNotes, EnableCheckProfile, TargetTriple, ProfilePrefix); bool FoundErrors = llvm::find_if(Errors, [](const ClangTidyError ) { 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 InputFiles, llvm::IntrusiveRefCntPtr BaseFS, bool ApplyAnyFix, bool EnableCheckProfile = false, + const std::string = "", 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 @@ -509,21 +509,23 @@ return Factory.getCheckOptions(); } -std::vector -runClangTidy(clang::tidy::ClangTidyContext , - const CompilationDatabase , - ArrayRef InputFiles, - llvm::IntrusiveRefCntPtr BaseFS, - bool ApplyAnyFix, bool EnableCheckProfile, - llvm::StringRef StoreCheckProfile) { +std::vector runClangTidy( +clang::tidy::ClangTidyContext , +const CompilationDatabase , ArrayRef InputFiles, +llvm::IntrusiveRefCntPtr BaseFS, +bool ApplyAnyFix, bool EnableCheckProfile, const std::string , +llvm::StringRef StoreCheckProfile) { ClangTool Tool(Compilations, InputFiles, std::make_shared(), BaseFS); // Add extra arguments passed by the clang-tidy command-line. ArgumentsAdjuster PerFileExtraArgumentsInserter = - [](const CommandLineArguments , StringRef Filename) { + [, ](const CommandLineArguments , +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("-")) ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D102322: [clang-tidy] Add '-target' CLI option to override target triple
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 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 Errors = runClangTidy(Context, OptionsParser->getCompilations(), PathList, BaseFS, - FixNotes, EnableCheckProfile, ProfilePrefix); + FixNotes, EnableCheckProfile, TargetTriple, ProfilePrefix); bool FoundErrors = llvm::find_if(Errors, [](const ClangTidyError ) { 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 InputFiles, llvm::IntrusiveRefCntPtr BaseFS, bool ApplyAnyFix, bool EnableCheckProfile = false, + const std::string = "", 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 InputFiles, llvm::IntrusiveRefCntPtr BaseFS, bool ApplyAnyFix, bool EnableCheckProfile, + const std::string , llvm::StringRef StoreCheckProfile) { ClangTool Tool(Compilations, InputFiles, std::make_shared(), BaseFS); // Add extra arguments passed by the clang-tidy command-line. ArgumentsAdjuster PerFileExtraArgumentsInserter = - [](const CommandLineArguments , StringRef Filename) { + [, ](const CommandLineArguments , 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() &&