Skip to content

Commit 611aa97

Browse files
committed
Add --exclude-directory=[path] to allow to filter-out files (from database) which are within some directories
1 parent 315238d commit 611aa97

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

.github/workflows/ubuntu.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ jobs:
7070
- name: run ccccc on itself
7171
run: |
7272
echo "require 'premake-export-compile-commands/export-compile-commands'" >> premake-system.lua
73-
premake5 --llvm-root="${{steps.LLVM.outputs.LLVM_ROOT}}" --expand-llvm-config --no-3rd export-compile-commands
74-
./bin/gmake2/Release/ccccc --template-file=template/ccccc_html/template.tpl project/export-compile-commands/release/compile_commands.json > index.html
73+
premake5 --llvm-root="${{steps.LLVM.outputs.LLVM_ROOT}}" --expand-llvm-config export-compile-commands
74+
./bin/gmake2/Release/ccccc --template-file=template/ccccc_html/template.tpl --exclude-directory=3rd project/export-compile-commands/release/compile_commands.json > index.html
7575
7676
- name: Upload index.html
7777
uses: actions/upload-artifact@v4

premake5.lua

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ newoption {
1212
description = "run llvm-config from premake directly"
1313
}
1414

15-
newoption {
16-
trigger = "no-3rd",
17-
description = "don't create 3rd party project"
18-
}
19-
2015
-- tinfo is not part of msys
2116
newoption {
2217
trigger = "without-tinfo",
@@ -29,7 +24,6 @@ end
2924

3025
local LLVMRoot = _OPTIONS["llvm-root"]
3126
local ExpandLLVMConfig = _OPTIONS["expand-llvm-config"]
32-
local No3rd = _OPTIONS["no-3rd"]
3327

3428
if (LLVMRoot == nil or LLVMRoot == "") then
3529
-- assume llvm is installed in system
@@ -98,7 +92,7 @@ solution "ccccc"
9892

9993
filter {}
10094
startproject "ccccc_app"
101-
if not No3rd then
95+
10296
-- --------------------------------------
10397
group "3rd"
10498
-- --------------------------------------
@@ -110,7 +104,7 @@ if not No3rd then
110104

111105
files { "%{ThirdRoot}/mstch/src/**.*", "%{ThirdRoot}/mstch/include/**.*" }
112106
includedirs { "%{ThirdRoot}/mstch/src", "%{ThirdRoot}/mstch/include" }
113-
end
107+
114108
-- --------------------------------------
115109
group "ccccc"
116110
-- --------------------------------------

src/lib/parameters.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** Copyright 2012-2022 Joris Dauphin
2+
** Copyright 2012-2024 Joris Dauphin
33
*/
44
/*
55
** This file is part of CCCCC.
@@ -28,12 +28,28 @@
2828

2929
#include <filesystem>
3030
#include <iostream>
31+
#include <string>
3132

3233
namespace ccccc
3334
{
3435

3536
namespace
3637
{
38+
bool is_subpath(const std::filesystem::path& path, const std::filesystem::path& base)
39+
{
40+
const auto rel = std::filesystem::relative(path, base);
41+
return !rel.empty() && rel.native()[0] != '.';
42+
}
43+
44+
bool withinDirectories(const std::filesystem::path& filename,
45+
const std::vector<std::filesystem::path>& directories)
46+
{
47+
return std::any_of(
48+
directories.begin(), directories.end(), [&](const std::filesystem::path& directory) {
49+
return is_subpath(filename, directory);
50+
});
51+
}
52+
3753
void ShowVersion(llvm::raw_ostream& os)
3854
{
3955
os << "CCCCC version 1.3\n";
@@ -52,12 +68,21 @@ void AddFilesFromDatabase(Parameters& parameters, std::filesystem::path compile_
5268
return;
5369
}
5470

71+
for (const auto& directory : parameters.GetExcludeDirectories()) {
72+
std::cerr << "Exclude directory: " << std::filesystem::path::abosulte(directory) << std::endl;
73+
}
74+
5575
CXCompileCommands commands = clang_CompilationDatabase_getAllCompileCommands(db);
5676
const unsigned size = clang_CompileCommands_getSize(commands);
5777
for (unsigned int i = 0; i != size; ++i) {
5878
const CXCompileCommand command = clang_CompileCommands_getCommand(commands, i);
59-
parameters.AddFile(
60-
use_clang::getStringAndDispose(clang_CompileCommand_getFilename(command)));
79+
auto filename = use_clang::getStringAndDispose(clang_CompileCommand_getFilename(command));
80+
if (!withinDirectories(filename, parameters.GetExcludeDirectories())) {
81+
std::cerr << "Add " << filename << std::endl;
82+
parameters.AddFile(filename);
83+
} else {
84+
std::cerr << "Exclude " << filename << std::endl;
85+
}
6186
}
6287

6388
clang_CompileCommands_dispose(commands);
@@ -112,6 +137,11 @@ void Parameters::Parse(const std::filesystem::path& cccccRoot, int argc, char**
112137
llvm::cl::init(std::filesystem::current_path().string())};
113138
llvm::cl::alias sourceRootAlias{
114139
"R", llvm::cl::desc("Alias for -source-root"), llvm::cl::aliasopt(sourceRoot)};
140+
llvm::cl::list<std::string> excludeDirectories{
141+
"exclude-directory",
142+
llvm::cl::desc("exclude input files from these directories"),
143+
llvm::cl::value_desc("exclude-directory"),
144+
llvm::cl::cat(cccccCategory)};
115145
llvm::cl::list<std::string> inputFilenames{
116146
llvm::cl::Positional, llvm::cl::desc("<input files>"), llvm::cl::OneOrMore};
117147

@@ -121,6 +151,10 @@ void Parameters::Parse(const std::filesystem::path& cccccRoot, int argc, char**
121151
llvm::cl::ParseCommandLineOptions(
122152
argc, argv, "Compute metrics from input files and output the report");
123153

154+
for (const auto& directory : excludeDirectories) {
155+
AddExcludeDirectory(directory);
156+
}
157+
124158
for (const std::filesystem::path f : inputFilenames) {
125159
if (f.filename() == "compile_commands.json") {
126160
if (GetDatabaseRoot().empty()) {
@@ -149,6 +183,7 @@ void Parameters::Parse(const std::filesystem::path& cccccRoot, int argc, char**
149183
defines.removeArgument();
150184
includes.removeArgument();
151185
inputFilenames.removeArgument();
186+
excludeDirectories.removeArgument();
152187
extraOptions.removeArgument();
153188
pch.removeArgument();
154189
sourceRoot.removeArgument();

src/lib/parameters.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** Copyright 2012-2022 Joris Dauphin
2+
** Copyright 2012-2024 Joris Dauphin
33
*/
44
/*
55
** This file is part of CCCCC.
@@ -40,6 +40,10 @@ class Parameters
4040
void AddExtra(const std::string& extra) { m_extras.push_back(extra); }
4141
void SetTemplateFilename(const std::filesystem::path& filename) { m_template = filename; }
4242
void SetDatabaseRoot(const std::filesystem::path& directory) { m_databaseRoot = directory; }
43+
void AddExcludeDirectory(const std::filesystem::path& directory)
44+
{
45+
m_excludeDirectories.push_back(directory);
46+
}
4347

4448
const std::filesystem::path& GetSourceRoot() const { return m_sourceRoot; }
4549
const std::vector<std::filesystem::path>& Filenames() const { return m_files; }
@@ -49,9 +53,11 @@ class Parameters
4953
const std::string& GetPch() const { return m_pch; }
5054
const std::filesystem::path& GetTemplateFilename() const { return m_template; }
5155
const std::filesystem::path& GetDatabaseRoot() const { return m_databaseRoot; }
56+
const std::vector<std::filesystem::path>& GetExcludeDirectories() const { return m_excludeDirectories; }
5257

5358
private:
5459
std::filesystem::path m_sourceRoot;
60+
std::vector<std::filesystem::path> m_excludeDirectories;
5561
std::vector<std::filesystem::path> m_files;
5662
std::vector<std::string> m_includePaths;
5763
std::vector<std::string> m_defines;

test/test_parameters.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** Copyright 2012-2022 Joris Dauphin
2+
** Copyright 2012-2024 Joris Dauphin
33
*/
44
/*
55
** This file is part of CCCCC.
@@ -75,6 +75,17 @@ TEST_CASE("PARAMETERS_ADD_PCH")
7575
CHECK(pchFile == param.GetPch());
7676
}
7777

78+
TEST_CASE("PARAMETERS_ADD_EXCLUDE_DIRECTORY")
79+
{
80+
const std::vector<std::filesystem::path> directories{"3rd", "submodules"};
81+
ccccc::Parameters param;
82+
83+
for (const auto& dir : directories) {
84+
param.AddExcludeDirectory(dir);
85+
}
86+
CHECK(directories == param.GetExcludeDirectories());
87+
}
88+
7889
TEST_CASE("PARAMETERS_PARSING_SHORT_OPTIONS")
7990
{
8091
const std::string cccccRoot = ".";
@@ -129,11 +140,14 @@ TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
129140
const char* extraFlag = "-extra-option";
130141
const char* pchFlag = "-pch";
131142
const char* templateFlag = "-template-file";
143+
const char* excludeDirFlag = "-exclude-directory";
132144
const std::vector<std::filesystem::path> files{"a.c", "a.h"};
133145
const std::vector<std::string> stringFiles{files[0].string(), files[1].string()};
134146
const std::vector<std::string> includes{"includeDir1", "includeDir2"};
135147
const std::vector<std::string> defines{"FOO", "BAR=42"};
136148
const std::vector<std::string> extras{"-std=c++14"};
149+
const std::vector<std::filesystem::path> dirs{"3rd", "exclude"};
150+
const std::vector<std::string> stringDirs{dirs[0].string(), dirs[1].string()};
137151
std::string pchFile = "pchFile";
138152
std::string templateFile = "templateFile";
139153
const char* argv[] = {argv0,
@@ -151,6 +165,10 @@ TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
151165
includes[1].c_str(),
152166
templateFlag,
153167
templateFile.c_str(),
168+
excludeDirFlag,
169+
stringDirs[0].c_str(),
170+
excludeDirFlag,
171+
stringDirs[1].c_str(),
154172
stringFiles[0].c_str(),
155173
stringFiles[1].c_str()};
156174
ccccc::Parameters param;
@@ -162,5 +180,6 @@ TEST_CASE("PARAMETERS_PARSING_LONG_OPTIONS")
162180
CHECK(defines == param.Defines());
163181
CHECK(extras == param.Extras());
164182
CHECK(pchFile == param.GetPch());
183+
CHECK(dirs == param.GetExcludeDirectories());
165184
CHECK(templateFile == param.GetTemplateFilename());
166185
}

0 commit comments

Comments
 (0)