Skip to content

[clang-tidy] support parameters file in command line #120547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions clang-tools-extra/clang-tidy/tool/ClangTidyMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
#include "../GlobList.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/WithColor.h"
#include "llvm/TargetParser/Host.h"
#include <optional>

using namespace clang::tooling;
Expand All @@ -36,6 +38,11 @@ static cl::desc desc(StringRef description) { return {description.ltrim()}; }
static cl::OptionCategory ClangTidyCategory("clang-tidy options");

static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp ClangTidyParameterFileHelp(R"(
Parameters files:
A large number of options or source files can be passed as parameter files
by use '@parameter-file' in the command line.
)");
static cl::extrahelp ClangTidyHelp(R"(
Configuration files:
clang-tidy attempts to read configuration for each source file from a
Expand Down Expand Up @@ -553,6 +560,21 @@ static llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> createBaseFS() {

int clangTidyMain(int argc, const char **argv) {
llvm::InitLLVM X(argc, argv);
SmallVector<const char *> Args{argv, argv + argc};

// expand parameters file to argc and argv.
llvm::BumpPtrAllocator Alloc;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a one-line comment describing what this block of code does/is meant for? Even better, can it be put into its own function?

Copy link
Contributor Author

@HerrCai0907 HerrCai0907 Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract to another function is less benefit because variable lifetime issue. The allocator must have same lifetime with argc
and argv.

llvm::cl::TokenizerCallback Tokenizer =
llvm::Triple(llvm::sys::getProcessTriple()).isOSWindows()
? llvm::cl::TokenizeWindowsCommandLine
: llvm::cl::TokenizeGNUCommandLine;
llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer);
if (llvm::Error Err = ECtx.expandResponseFiles(Args)) {
llvm::WithColor::error() << llvm::toString(std::move(Err)) << "\n";
return 1;
}
argc = static_cast<int>(Args.size());
argv = Args.data();

// Enable help for -load option, if plugins are enabled.
if (cl::Option *LoadOpt = cl::getRegisteredOptions().lookup("load"))
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ Improvements to clang-tidy
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
happening on certain platforms when interrupting the script.

- Improved :program:`clang-tidy` by accepting parameters file in command line.

- Removed :program:`clang-tidy`'s global options for most of checks. All options
are changed to local options except `IncludeStyle`, `StrictMode` and
`IgnoreMacros`.
Expand Down
11 changes: 11 additions & 0 deletions clang-tools-extra/docs/clang-tidy/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ compilation options on the command line after ``--``:
$ clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...
If there are too many options or source files to specify on the command line,
you can store them in a parameter file, and use :program:`clang-tidy` with that
parameters file:

.. code-block:: console
$ clang-tidy @parameters_file
:program:`clang-tidy` has its own checks and can also run Clang Static Analyzer
checks. Each check has a name and the checks to run can be chosen using the
``-checks=`` option, which specifies a comma-separated list of positive and
Expand Down Expand Up @@ -264,6 +272,9 @@ An overview of all the command-line options:
automatically removed, but the rest of a relative path must be a
suffix of a path in the compile command database.
Parameters files:
A large number of options or source files can be passed as parameter files
by use '@parameter-file' in the command line.
Configuration files:
clang-tidy attempts to read configuration for each source file from a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-checks='-*,llvm-namespace-comment'
--warnings-as-errors=llvm-namespace-comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// RUN: echo @%t.param > %t.param && not clang-tidy %s @%t.param -- 2>&1 | FileCheck %s

// CHECK: recursive expansion of
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// RUN: not clang-tidy %s @%S/Inputs/param/parameters.txt -- | FileCheck %s

namespace i {
}
// CHECK: error: namespace 'i' not terminated with a closing comment [llvm-namespace-comment,-warnings-as-errors]
Loading