23
23
#include " clang/Lex/HeaderSearch.h"
24
24
#include " clang/Lex/Preprocessor.h"
25
25
#include " clang/Tooling/Syntax/Tokens.h"
26
+ #include " llvm/ADT/ArrayRef.h"
26
27
#include " llvm/ADT/STLFunctionalExtras.h"
28
+ #include " llvm/ADT/SmallString.h"
27
29
#include " llvm/ADT/StringSet.h"
28
30
#include " llvm/Support/FormatVariadic.h"
29
31
#include " llvm/Support/Path.h"
32
+ #include " llvm/Support/Regex.h"
33
+ #include < functional>
30
34
31
35
namespace clang {
32
36
namespace clangd {
@@ -233,7 +237,8 @@ void findReferencedMacros(const SourceManager &SM, Preprocessor &PP,
233
237
}
234
238
}
235
239
236
- static bool mayConsiderUnused (const Inclusion &Inc, ParsedAST &AST) {
240
+ static bool mayConsiderUnused (const Inclusion &Inc, ParsedAST &AST,
241
+ const Config &Cfg) {
237
242
if (Inc.BehindPragmaKeep )
238
243
return false ;
239
244
@@ -258,6 +263,15 @@ static bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST) {
258
263
FE->getName ());
259
264
return false ;
260
265
}
266
+ for (auto &Filter : Cfg.Diagnostics .Includes .IgnoreHeader ) {
267
+ // Convert the path to Unix slashes and try to match aginast the fiilter.
268
+ llvm::SmallString<64 > Path (Inc.Resolved );
269
+ llvm::sys::path::native (Path, llvm::sys::path::Style ::posix);
270
+ if (Filter (Inc.Resolved )) {
271
+ dlog (" {0} header is filtered out by the configuration" , FE->getName ());
272
+ return false ;
273
+ }
274
+ }
261
275
return true ;
262
276
}
263
277
@@ -369,6 +383,7 @@ getUnused(ParsedAST &AST,
369
383
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles,
370
384
const llvm::StringSet<> &ReferencedPublicHeaders) {
371
385
trace::Span Tracer (" IncludeCleaner::getUnused" );
386
+ const Config &Cfg = Config::current ();
372
387
std::vector<const Inclusion *> Unused;
373
388
for (const Inclusion &MFI : AST.getIncludeStructure ().MainFileIncludes ) {
374
389
if (!MFI.HeaderID )
@@ -377,7 +392,7 @@ getUnused(ParsedAST &AST,
377
392
continue ;
378
393
auto IncludeID = static_cast <IncludeStructure::HeaderID>(*MFI.HeaderID );
379
394
bool Used = ReferencedFiles.contains (IncludeID);
380
- if (!Used && !mayConsiderUnused (MFI, AST)) {
395
+ if (!Used && !mayConsiderUnused (MFI, AST, Cfg )) {
381
396
dlog (" {0} was not used, but is not eligible to be diagnosed as unused" ,
382
397
MFI.Written );
383
398
continue ;
0 commit comments