11/*
2- ** Copyright 2012-2022 Joris Dauphin
2+ ** Copyright 2012-2024 Joris Dauphin
33*/
44/*
55** This file is part of CCCCC.
2828
2929#include < filesystem>
3030#include < iostream>
31+ #include < string>
3132
3233namespace ccccc
3334{
3435
3536namespace
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+
3753void 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 ();
0 commit comments