Skip to content

Commit

Permalink
Add option category for ccccc --help
Browse files Browse the repository at this point in the history
Make `Parameters::Parse` reentrant.
  • Loading branch information
Jarod42 committed May 24, 2024
1 parent 3a7a6f5 commit 5afcd8f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
46 changes: 36 additions & 10 deletions src/lib/parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,56 @@ void AddFilesFromDatabase(Parameters& parameters, std::filesystem::path compile_

void Parameters::Parse(const std::filesystem::path& cccccRoot, int argc, char** argv)
{
llvm::cl::list<std::string> defines{
"define", llvm::cl::desc("Specify define"), llvm::cl::value_desc("define")};
llvm::cl::OptionCategory cccccCategory{"ccccc Options"};

llvm::cl::list<std::string> defines{"define",
llvm::cl::desc("Specify define"),
llvm::cl::value_desc("define"),
llvm::cl::cat(cccccCategory)};
llvm::cl::alias defineAlias{
"D", llvm::cl::desc("Alias for -define"), llvm::cl::aliasopt(defines)};

llvm::cl::list<std::string> extraOptions{
"extra-option",
llvm::cl::desc("Extra option directly given to the clang parser"),
llvm::cl::value_desc("extra-option")};
llvm::cl::value_desc("extra-option"),
llvm::cl::cat(cccccCategory)};
llvm::cl::alias extraAlias{
"e", llvm::cl::desc("Alias for -extra-option"), llvm::cl::aliasopt(extraOptions)};
llvm::cl::list<std::string> includes{
"include-dir", llvm::cl::desc("Specify include path"), llvm::cl::value_desc("path")};
llvm::cl::list<std::string> includes{"include-dir",
llvm::cl::desc("Specify include path"),
llvm::cl::value_desc("path"),
llvm::cl::cat(cccccCategory)};
llvm::cl::alias includeAlias{
"I", llvm::cl::desc("Alias for -include-dir"), llvm::cl::aliasopt(includes)};
llvm::cl::opt<std::string> templateFile{
"template-file",
llvm::cl::desc(
"template file to use for the report (default is template/html/template.tpl)"),
llvm::cl::value_desc("template-file"),
llvm::cl::cat(cccccCategory),
llvm::cl::init((cccccRoot / "template/html/template.tpl").string())};
llvm::cl::alias includeTemplate{
llvm::cl::alias templateFileAlias{
"t", llvm::cl::desc("Alias for -template-file"), llvm::cl::aliasopt(templateFile)};
llvm::cl::opt<std::string> pch{
"pch", llvm::cl::desc("Compiled header path"), llvm::cl::value_desc("pch-file")};
llvm::cl::opt<std::string> pch{"pch",
llvm::cl::desc("Compiled header path"),
llvm::cl::value_desc("pch-file"),
llvm::cl::cat(cccccCategory)};
llvm::cl::opt<std::string> sourceRoot{
"source-root",
llvm::cl::desc("source root directory (filename display would be relative to that path) "
"(default is current working directory)"),
llvm::cl::value_desc("source-root"),
llvm::cl::cat(cccccCategory),
llvm::cl::init(std::filesystem::current_path().string())};
llvm::cl::alias sourceRootAlias{
"R", llvm::cl::desc("Alias for -source-root"), llvm::cl::aliasopt(sourceRoot)};

llvm::cl::list<std::string> inputFilenames{
llvm::cl::Positional, llvm::cl::desc("<input files>"), llvm::cl::OneOrMore};
llvm::cl::SetVersionPrinter(ShowVersion);

HideUnrelatedOptions(cccccCategory);

llvm::cl::AddExtraVersionPrinter(ShowVersion);
llvm::cl::ParseCommandLineOptions(
argc, argv, "Compute metrics from input files and output the report");

Expand Down Expand Up @@ -132,6 +145,19 @@ void Parameters::Parse(const std::filesystem::path& cccccRoot, int argc, char**
}
SetSourceRoot(std::string(sourceRoot));
SetTemplateFilename(std::string(templateFile));

defines.removeArgument();
includes.removeArgument();
inputFilenames.removeArgument();
extraOptions.removeArgument();
pch.removeArgument();
sourceRoot.removeArgument();
templateFile.removeArgument();
defineAlias.removeArgument();
includeAlias.removeArgument();
extraAlias.removeArgument();
sourceRootAlias.removeArgument();
templateFileAlias.removeArgument();
}

} // namespace ccccc
19 changes: 2 additions & 17 deletions test/test_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@

#include <doctest.h>

template <typename T, std::size_t N>
static constexpr std::size_t ArraySize(const T (&)[N])
{
return N;
}

TEST_CASE("PARAMETERS_ADD_FILE")
{
const std::vector<std::filesystem::path> files{"a.c", "a.h", "b.c", "b.h"};
Expand Down Expand Up @@ -81,9 +75,6 @@ TEST_CASE("PARAMETERS_ADD_PCH")
CHECK(pchFile == param.GetPch());
}

#if 0 // param.Parse is not reentrant :/
// because of llvm::cl::ParseCommandLineOptions

TEST_CASE("PARAMETERS_PARSING_SHORT_OPTIONS")
{
const std::string cccccRoot = ".";
Expand Down Expand Up @@ -119,7 +110,7 @@ TEST_CASE("PARAMETERS_PARSING_SHORT_OPTIONS")
stringFiles[1].c_str()};
ccccc::Parameters param;

param.Parse(cccccRoot, ArraySize(argv), const_cast<char**>(argv));
param.Parse(cccccRoot, std::size(argv), const_cast<char**>(argv));

CHECK(files == param.Filenames());
CHECK(includes == param.IncludePaths());
Expand All @@ -129,11 +120,6 @@ TEST_CASE("PARAMETERS_PARSING_SHORT_OPTIONS")
CHECK(templateFile == param.GetTemplateFilename());
}

#endif

#if 0 // param.Parse is not reentrant :/
// because of llvm::cl::ParseCommandLineOptions

TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
{
const std::string cccccRoot = ".";
Expand Down Expand Up @@ -169,7 +155,7 @@ TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
stringFiles[1].c_str()};
ccccc::Parameters param;

param.Parse(cccccRoot, ArraySize(argv), const_cast<char**>(argv));
param.Parse(cccccRoot, std::size(argv), const_cast<char**>(argv));

CHECK(files == param.Filenames());
CHECK(includes == param.IncludePaths());
Expand All @@ -178,4 +164,3 @@ TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
CHECK(pchFile == param.GetPch());
CHECK(templateFile == param.GetTemplateFilename());
}
#endif

0 comments on commit 5afcd8f

Please sign in to comment.