forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclang_include_cleaner.patch
2235 lines (2200 loc) · 79.8 KB
/
clang_include_cleaner.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ported from clangd, this still can be improved over time, but it can be landed.
This was based on the work from https://bit.ly/3TkV2N1
The utility makes the assumption that all header are self contained!
It only checkes Decls from the main translation file, where SourceLocarion is the passed cpp file.
It builds a list with all of the includes from the translation unit.
It matches all of the Decls from the main translation units with definitions from the included header files and builds a list with used header files.
All of the includes that are not part of the matched used header files are considered to be unused. Of course this is correct if the first assumption if followed by the coding guide, where all of the header are self contained. Since the mozilla code base doesn't follow this approach false positives might appear where the is the following situation:
FOO.cpp
#include <A>
#Include <B>
If header A defines a symbol that is used by header B and B doesn't include A nor
it has symbols defined that are used by FOO.cpp then B it will be marked as potentially to be removed
by the tool.
This is the limitation determined by header that are not self contained.
The limitation presented above can be fixed in the future with extra work, but it's very time expensive
during the runtime of the checker.
diff --git a/clang-tools-extra/CMakeLists.txt b/clang-tools-extra/CMakeLists.txt
index 6a3f741721ee..ff17c8e8472a 100644
--- a/clang-tools-extra/CMakeLists.txt
+++ b/clang-tools-extra/CMakeLists.txt
@@ -16,6 +16,7 @@ endif()
add_subdirectory(clang-apply-replacements)
add_subdirectory(clang-reorder-fields)
add_subdirectory(modularize)
+add_subdirectory(include-cleaner)
add_subdirectory(clang-tidy)
add_subdirectory(clang-change-namespace)
@@ -23,7 +24,6 @@ add_subdirectory(clang-doc)
add_subdirectory(clang-include-fixer)
add_subdirectory(clang-move)
add_subdirectory(clang-query)
-add_subdirectory(include-cleaner)
add_subdirectory(pp-trace)
add_subdirectory(pseudo)
add_subdirectory(tool-template)
diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt b/clang-tools-extra/clang-tidy/CMakeLists.txt
index 8a953eeea275..f2edc509acaf 100644
--- a/clang-tools-extra/clang-tidy/CMakeLists.txt
+++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
@@ -50,6 +50,7 @@ endif()
# Checks.
# If you add a check, also add it to ClangTidyForceLinker.h in this directory.
+add_subdirectory(alpha)
add_subdirectory(android)
add_subdirectory(abseil)
add_subdirectory(altera)
@@ -77,6 +78,7 @@ add_subdirectory(portability)
add_subdirectory(readability)
add_subdirectory(zircon)
set(ALL_CLANG_TIDY_CHECKS
+ clangTidyAlphaModule
clangTidyAndroidModule
clangTidyAbseilModule
clangTidyAlteraModule
diff --git a/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h b/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
index 2691d90fa521..2fa064cff22a 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
+++ b/clang-tools-extra/clang-tidy/ClangTidyForceLinker.h
@@ -20,6 +20,11 @@ extern volatile int AbseilModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED AbseilModuleAnchorDestination =
AbseilModuleAnchorSource;
+// This anchor is used to force the linker to link the AlphaModule.
+extern volatile int AlphaModuleAnchorSource;
+static int LLVM_ATTRIBUTE_UNUSED AlphaModuleAnchorDestination =
+ AlphaModuleAnchorSource;
+
// This anchor is used to force the linker to link the AlteraModule.
extern volatile int AlteraModuleAnchorSource;
static int LLVM_ATTRIBUTE_UNUSED AlteraModuleAnchorDestination =
diff --git a/clang-tools-extra/clang-tidy/alpha/AlphaTidyModule.cpp b/clang-tools-extra/clang-tidy/alpha/AlphaTidyModule.cpp
new file mode 100644
index 000000000000..b598a36cebf7
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/alpha/AlphaTidyModule.cpp
@@ -0,0 +1,38 @@
+//===--- AlphaTidyModule.cpp - clang-tidy ----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "../ClangTidy.h"
+#include "../ClangTidyModule.h"
+#include "../ClangTidyModuleRegistry.h"
+#include "UnusedIncludesCheck.h"
+
+
+namespace clang {
+namespace tidy {
+namespace alpha {
+
+class AlphaModule : public ClangTidyModule {
+public:
+ void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+
+ CheckFactories.registerCheck<UnusedIncludesCheck>("alpha-unused-includes");
+ }
+};
+
+} // namespace alpha
+
+// Register the AlphaTidyModule using this statically initialized variable.
+static ClangTidyModuleRegistry::Add<alpha::AlphaModule>
+ X("alpha-module", "Adds alpha lint checks.");
+
+// This anchor is used to force the linker to link in the generated object file
+// and thus register the AlphaModule.
+volatile int AlphaModuleAnchorSource = 0;
+
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/alpha/CMakeLists.txt b/clang-tools-extra/clang-tidy/alpha/CMakeLists.txt
new file mode 100644
index 000000000000..b50576868645
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/alpha/CMakeLists.txt
@@ -0,0 +1,32 @@
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../include-cleaner/include)
+
+set(LLVM_LINK_COMPONENTS
+ Support
+ )
+
+add_clang_library(clangTidyAlphaModule
+
+ AlphaTidyModule.cpp
+ UnusedIncludesCheck.cpp
+
+ LINK_LIBS
+ clangAnalysis
+ clangIncludeCleaner
+ clangTidy
+ clangTidyUtils
+
+ DEPENDS
+ omp_gen
+ )
+
+clang_target_link_libraries(clangTidyAlphaModule
+ PRIVATE
+ clangAnalysis
+ clangAST
+ clangASTMatchers
+ clangBasic
+ clangIncludeCleaner
+ clangLex
+ clangSerialization
+ clangTooling
+ )
diff --git a/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.cpp b/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.cpp
new file mode 100644
index 000000000000..0d6a6bf7a367
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.cpp
@@ -0,0 +1,76 @@
+//===--- UnusedIncludesCheck.cpp - clang-tidy------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UnusedIncludesCheck.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Hooks.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Lex/Preprocessor.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace alpha {
+
+UnusedIncludesCheck::UnusedIncludesCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+
+void UnusedIncludesCheck::registerPPCallbacks(const SourceManager &SM,
+ Preprocessor *PP,
+ Preprocessor *) {
+ Ctx = std::make_unique<include_cleaner::AnalysisContext>(
+ include_cleaner::Policy{}, *PP);
+ RecordedPP = std::make_unique<include_cleaner::RecordedPP>();
+ PP->addPPCallbacks(RecordedPP->record(*Ctx));
+}
+
+void UnusedIncludesCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ translationUnitDecl(forEach(decl(isExpansionInMainFile()).bind("top"))),
+ this);
+}
+
+void UnusedIncludesCheck::check(const MatchFinder::MatchResult &Result) {
+ Top.push_back(const_cast<Decl *>(Result.Nodes.getNodeAs<Decl>("top")));
+}
+
+void UnusedIncludesCheck::onEndOfTranslationUnit() {
+ llvm::DenseSet<const include_cleaner::RecordedPP::Include *> Used;
+ llvm::DenseSet<include_cleaner::Header> Seen;
+ include_cleaner::walkUsed(
+ *Ctx, Top, RecordedPP->MacroReferences,
+ [&](SourceLocation Loc, include_cleaner::Symbol Sym,
+ llvm::ArrayRef<include_cleaner::Header> Headers) {
+ for (const auto &Header : Headers) {
+ if (!Seen.insert(Header).second)
+ continue;
+ const auto& HeadersToInsert = RecordedPP->Includes.match(Header);
+ Used.insert(HeadersToInsert.begin(), HeadersToInsert.end());
+ }
+ });
+ for (const auto &I : RecordedPP->Includes.all()) {
+ if (!Used.contains(&I)) {
+ const auto &SM = Ctx->sourceManager();
+ FileID FID = SM.getFileID(I.Location);
+ diag(I.Location, "there is a high probability that include is unused")
+ << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
+ SM.translateLineCol(FID, I.Line, 1),
+ SM.translateLineCol(FID, I.Line + 1, 1)));
+ }
+ }
+}
+
+UnusedIncludesCheck::~UnusedIncludesCheck() = default;
+
+} // namespace alpha
+} // namespace tidy
+} // namespace clang
diff --git a/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.h b/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.h
new file mode 100644
index 000000000000..f67c46e6cc3e
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/alpha/UnusedIncludesCheck.h
@@ -0,0 +1,42 @@
+//===--- UnusedIncludesCheck.h - clang-tidy----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_INCLUDES_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_INCLUDES_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang {
+namespace include_cleaner {
+class AnalysisContext;
+struct RecordedPP;
+} // namespace include_cleaner
+namespace tidy {
+namespace alpha {
+
+class UnusedIncludesCheck : public ClangTidyCheck {
+public:
+ UnusedIncludesCheck(StringRef Name, ClangTidyContext *Context);
+ ~UnusedIncludesCheck();
+ void registerPPCallbacks(const SourceManager &SM, Preprocessor *,
+ Preprocessor *) override;
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ void onEndOfTranslationUnit() override;
+
+private:
+ std::unique_ptr<include_cleaner::AnalysisContext> Ctx;
+ std::unique_ptr<include_cleaner::RecordedPP> RecordedPP;
+ std::vector<Decl *> Top;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_UNUSED_INCLUDES_H
diff --git a/clang-tools-extra/clangd/CMakeLists.txt b/clang-tools-extra/clangd/CMakeLists.txt
index de8f087a52a5..14f605b1efaf 100644
--- a/clang-tools-extra/clangd/CMakeLists.txt
+++ b/clang-tools-extra/clangd/CMakeLists.txt
@@ -2,6 +2,8 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
+include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include-cleaner/include)
+
add_subdirectory(support)
# Configure the Features.inc file.
@@ -153,6 +155,7 @@ clang_target_link_libraries(clangDaemon
clangDriver
clangFormat
clangFrontend
+ clangIncludeCleaner
clangIndex
clangLex
clangSema
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index 26eb2574195d..a3cbc8894f6d 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -12,9 +12,11 @@
#include "CodeCompletionStrings.h"
#include "Config.h"
#include "FindTarget.h"
+#include "IncludeCleaner.h"
#include "ParsedAST.h"
#include "Selection.h"
#include "SourceCode.h"
+#include "clang-include-cleaner/Analysis.h"
#include "index/SymbolCollector.h"
#include "support/Markup.h"
#include "clang/AST/ASTContext.h"
@@ -985,6 +987,23 @@ llvm::Optional<HoverInfo> getHover(ParsedAST &AST, Position Pos,
// FIXME: We don't have a fitting value for Kind.
HI.Definition =
URIForFile::canonicalize(Inc.Resolved, *MainFilePath).file().str();
+
+ // FIXME: share code, macros too...
+ include_cleaner::AnalysisContext Ctx(include_cleaner::Policy{},
+ AST.getPreprocessor());
+ std::vector<std::string> Provides;
+ include_cleaner::walkUsed(
+ Ctx, AST.getLocalTopLevelDecls(), /*Macros=*/{},
+ [&](SourceLocation Loc, include_cleaner::Symbol S,
+ llvm::ArrayRef<include_cleaner::Header> Headers) {
+ for (const auto &H : Headers)
+ if (match(H, Inc, AST.getIncludeStructure()))
+ Provides.push_back(S.name());
+ });
+ llvm::sort(Provides);
+ Provides.erase(std::unique(Provides.begin(), Provides.end()),
+ Provides.end());
+ HI.Documentation = "provides " + llvm::join(Provides, ", ");
HI.DefinitionLanguage = "";
return HI;
}
diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index e5b5187e030c..3c0ba06316ac 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -12,6 +12,8 @@
#include "ParsedAST.h"
#include "Protocol.h"
#include "SourceCode.h"
+#include "clang-include-cleaner/Analysis.h"
+#include "clang-include-cleaner/Types.h"
#include "index/CanonicalIncludes.h"
#include "support/Logger.h"
#include "support/Trace.h"
@@ -40,181 +42,6 @@ void setIncludeCleanerAnalyzesStdlib(bool B) { AnalyzeStdlib = B; }
namespace {
-/// Crawler traverses the AST and feeds in the locations of (sometimes
-/// implicitly) used symbols into \p Result.
-class ReferencedLocationCrawler
- : public RecursiveASTVisitor<ReferencedLocationCrawler> {
-public:
- ReferencedLocationCrawler(ReferencedLocations &Result,
- const SourceManager &SM)
- : Result(Result), SM(SM) {}
-
- bool VisitDeclRefExpr(DeclRefExpr *DRE) {
- add(DRE->getDecl());
- add(DRE->getFoundDecl());
- return true;
- }
-
- bool VisitMemberExpr(MemberExpr *ME) {
- add(ME->getMemberDecl());
- add(ME->getFoundDecl().getDecl());
- return true;
- }
-
- bool VisitTagType(TagType *TT) {
- add(TT->getDecl());
- return true;
- }
-
- bool VisitFunctionDecl(FunctionDecl *FD) {
- // Function definition will require redeclarations to be included.
- if (FD->isThisDeclarationADefinition())
- add(FD);
- return true;
- }
-
- bool VisitCXXConstructExpr(CXXConstructExpr *CCE) {
- add(CCE->getConstructor());
- return true;
- }
-
- bool VisitTemplateSpecializationType(TemplateSpecializationType *TST) {
- // Using templateName case is handled by the override TraverseTemplateName.
- if (TST->getTemplateName().getKind() == TemplateName::UsingTemplate)
- return true;
- add(TST->getAsCXXRecordDecl()); // Specialization
- return true;
- }
-
- // There is no VisitTemplateName in RAV, thus we override the Traverse version
- // to handle the Using TemplateName case.
- bool TraverseTemplateName(TemplateName TN) {
- VisitTemplateName(TN);
- return Base::TraverseTemplateName(TN);
- }
- // A pseudo VisitTemplateName, dispatched by the above TraverseTemplateName!
- bool VisitTemplateName(TemplateName TN) {
- if (const auto *USD = TN.getAsUsingShadowDecl()) {
- add(USD);
- return true;
- }
- add(TN.getAsTemplateDecl()); // Primary template.
- return true;
- }
-
- bool VisitUsingType(UsingType *UT) {
- add(UT->getFoundDecl());
- return true;
- }
-
- bool VisitTypedefType(TypedefType *TT) {
- add(TT->getDecl());
- return true;
- }
-
- // Consider types of any subexpression used, even if the type is not named.
- // This is helpful in getFoo().bar(), where Foo must be complete.
- // FIXME(kirillbobyrev): Should we tweak this? It may not be desirable to
- // consider types "used" when they are not directly spelled in code.
- bool VisitExpr(Expr *E) {
- TraverseType(E->getType());
- return true;
- }
-
- bool TraverseType(QualType T) {
- if (isNew(T.getTypePtrOrNull())) // don't care about quals
- Base::TraverseType(T);
- return true;
- }
-
- bool VisitUsingDecl(UsingDecl *D) {
- for (const auto *Shadow : D->shadows())
- add(Shadow->getTargetDecl());
- return true;
- }
-
- // Enums may be usefully forward-declared as *complete* types by specifying
- // an underlying type. In this case, the definition should see the declaration
- // so they can be checked for compatibility.
- bool VisitEnumDecl(EnumDecl *D) {
- if (D->isThisDeclarationADefinition() && D->getIntegerTypeSourceInfo())
- add(D);
- return true;
- }
-
- // When the overload is not resolved yet, mark all candidates as used.
- bool VisitOverloadExpr(OverloadExpr *E) {
- for (const auto *ResolutionDecl : E->decls())
- add(ResolutionDecl);
- return true;
- }
-
-private:
- using Base = RecursiveASTVisitor<ReferencedLocationCrawler>;
-
- void add(const Decl *D) {
- if (!D || !isNew(D->getCanonicalDecl()))
- return;
- if (auto SS = StdRecognizer(D)) {
- Result.Stdlib.insert(*SS);
- return;
- }
- // Special case RecordDecls, as it is common for them to be forward
- // declared multiple times. The most common cases are:
- // - Definition available in TU, only mark that one as usage. The rest is
- // likely to be unnecessary. This might result in false positives when an
- // internal definition is visible.
- // - There's a forward declaration in the main file, no need for other
- // redecls.
- if (const auto *RD = llvm::dyn_cast<RecordDecl>(D)) {
- if (const auto *Definition = RD->getDefinition()) {
- Result.User.insert(Definition->getLocation());
- return;
- }
- if (SM.isInMainFile(RD->getMostRecentDecl()->getLocation()))
- return;
- }
- for (const Decl *Redecl : D->redecls())
- Result.User.insert(Redecl->getLocation());
- }
-
- bool isNew(const void *P) { return P && Visited.insert(P).second; }
-
- ReferencedLocations &Result;
- llvm::DenseSet<const void *> Visited;
- const SourceManager &SM;
- tooling::stdlib::Recognizer StdRecognizer;
-};
-
-// Given a set of referenced FileIDs, determines all the potentially-referenced
-// files and macros by traversing expansion/spelling locations of macro IDs.
-// This is used to map the referenced SourceLocations onto real files.
-struct ReferencedFilesBuilder {
- ReferencedFilesBuilder(const SourceManager &SM) : SM(SM) {}
- llvm::DenseSet<FileID> Files;
- llvm::DenseSet<FileID> Macros;
- const SourceManager &SM;
-
- void add(SourceLocation Loc) { add(SM.getFileID(Loc), Loc); }
-
- void add(FileID FID, SourceLocation Loc) {
- if (FID.isInvalid())
- return;
- assert(SM.isInFileID(Loc, FID));
- if (Loc.isFileID()) {
- Files.insert(FID);
- return;
- }
- // Don't process the same macro FID twice.
- if (!Macros.insert(FID).second)
- return;
- const auto &Exp = SM.getSLocEntry(FID).getExpansion();
- add(Exp.getSpellingLoc());
- add(Exp.getExpansionLocStart());
- add(Exp.getExpansionLocEnd());
- }
-};
-
// Returns the range starting at '#' and ending at EOL. Escaped newlines are not
// handled.
clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) {
@@ -231,10 +58,10 @@ clangd::Range getDiagnosticRange(llvm::StringRef Code, unsigned HashOffset) {
// Finds locations of macros referenced from within the main file. That includes
// references that were not yet expanded, e.g `BAR` in `#define FOO BAR`.
-void findReferencedMacros(const SourceManager &SM, Preprocessor &PP,
- const syntax::TokenBuffer *Tokens,
- ReferencedLocations &Result) {
+std::vector<include_cleaner::SymbolReference>
+findReferencedMacros(ParsedAST &AST, include_cleaner::AnalysisContext &Ctx) {
trace::Span Tracer("IncludeCleaner::findReferencedMacros");
+ std::vector<include_cleaner::SymbolReference> Result;
// FIXME(kirillbobyrev): The macros from the main file are collected in
// ParsedAST's MainFileMacros. However, we can't use it here because it
// doesn't handle macro references that were not expanded, e.g. in macro
@@ -244,15 +71,19 @@ void findReferencedMacros(const SourceManager &SM, Preprocessor &PP,
// this mechanism (as opposed to iterating through all tokens) will improve
// the performance of findReferencedMacros and also improve other features
// relying on MainFileMacros.
- for (const syntax::Token &Tok : Tokens->spelledTokens(SM.getMainFileID())) {
- auto Macro = locateMacroAt(Tok, PP);
+ for (const syntax::Token &Tok :
+ AST.getTokens().spelledTokens(AST.getSourceManager().getMainFileID())) {
+ auto Macro = locateMacroAt(Tok, AST.getPreprocessor());
if (!Macro)
continue;
auto Loc = Macro->Info->getDefinitionLoc();
if (Loc.isValid())
- Result.User.insert(Loc);
- // FIXME: support stdlib macros
+ Result.push_back(include_cleaner::SymbolReference{
+ Tok.location(),
+ Ctx.macro(AST.getPreprocessor().getIdentifierInfo(Macro->Name),
+ Loc)});
}
+ return Result;
}
static bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST,
@@ -296,110 +127,8 @@ static bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST,
}
return true;
}
-
-// In case symbols are coming from non self-contained header, we need to find
-// its first includer that is self-contained. This is the header users can
-// include, so it will be responsible for bringing the symbols from given
-// header into the scope.
-FileID headerResponsible(FileID ID, const SourceManager &SM,
- const IncludeStructure &Includes) {
- // Unroll the chain of non self-contained headers until we find the one that
- // can be included.
- for (const FileEntry *FE = SM.getFileEntryForID(ID); ID != SM.getMainFileID();
- FE = SM.getFileEntryForID(ID)) {
- // If FE is nullptr, we consider it to be the responsible header.
- if (!FE)
- break;
- auto HID = Includes.getID(FE);
- assert(HID && "We're iterating over headers already existing in "
- "IncludeStructure");
- if (Includes.isSelfContained(*HID))
- break;
- // The header is not self-contained: put the responsibility for its symbols
- // on its includer.
- ID = SM.getFileID(SM.getIncludeLoc(ID));
- }
- return ID;
-}
-
} // namespace
-ReferencedLocations findReferencedLocations(ASTContext &Ctx, Preprocessor &PP,
- const syntax::TokenBuffer *Tokens) {
- trace::Span Tracer("IncludeCleaner::findReferencedLocations");
- ReferencedLocations Result;
- const auto &SM = Ctx.getSourceManager();
- ReferencedLocationCrawler Crawler(Result, SM);
- Crawler.TraverseAST(Ctx);
- if (Tokens)
- findReferencedMacros(SM, PP, Tokens, Result);
- return Result;
-}
-
-ReferencedLocations findReferencedLocations(ParsedAST &AST) {
- return findReferencedLocations(AST.getASTContext(), AST.getPreprocessor(),
- &AST.getTokens());
-}
-
-ReferencedFiles findReferencedFiles(
- const ReferencedLocations &Locs, const SourceManager &SM,
- llvm::function_ref<FileID(FileID)> HeaderResponsible,
- llvm::function_ref<Optional<StringRef>(FileID)> UmbrellaHeader) {
- std::vector<SourceLocation> Sorted{Locs.User.begin(), Locs.User.end()};
- llvm::sort(Sorted); // Group by FileID.
- ReferencedFilesBuilder Builder(SM);
- for (auto It = Sorted.begin(); It < Sorted.end();) {
- FileID FID = SM.getFileID(*It);
- Builder.add(FID, *It);
- // Cheaply skip over all the other locations from the same FileID.
- // This avoids lots of redundant Loc->File lookups for the same file.
- do
- ++It;
- while (It != Sorted.end() && SM.isInFileID(*It, FID));
- }
-
- // If a header is not self-contained, we consider its symbols a logical part
- // of the including file. Therefore, mark the parents of all used
- // non-self-contained FileIDs as used. Perform this on FileIDs rather than
- // HeaderIDs, as each inclusion of a non-self-contained file is distinct.
- llvm::DenseSet<FileID> UserFiles;
- llvm::StringSet<> PublicHeaders;
- for (FileID ID : Builder.Files) {
- UserFiles.insert(HeaderResponsible(ID));
- if (auto PublicHeader = UmbrellaHeader(ID)) {
- PublicHeaders.insert(*PublicHeader);
- }
- }
-
- llvm::DenseSet<tooling::stdlib::Header> StdlibFiles;
- for (const auto &Symbol : Locs.Stdlib)
- for (const auto &Header : Symbol.headers())
- StdlibFiles.insert(Header);
-
- return {std::move(UserFiles), std::move(StdlibFiles),
- std::move(PublicHeaders)};
-}
-
-ReferencedFiles findReferencedFiles(const ReferencedLocations &Locs,
- const IncludeStructure &Includes,
- const CanonicalIncludes &CanonIncludes,
- const SourceManager &SM) {
- return findReferencedFiles(
- Locs, SM,
- [&SM, &Includes](FileID ID) {
- return headerResponsible(ID, SM, Includes);
- },
- [&SM, &CanonIncludes](FileID ID) -> Optional<StringRef> {
- auto Entry = SM.getFileEntryRefForID(ID);
- if (!Entry)
- return llvm::None;
- auto PublicHeader = CanonIncludes.mapHeader(*Entry);
- if (PublicHeader.empty())
- return llvm::None;
- return PublicHeader;
- });
-}
-
std::vector<const Inclusion *>
getUnused(ParsedAST &AST,
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles,
@@ -426,51 +155,50 @@ getUnused(ParsedAST &AST,
return Unused;
}
-#ifndef NDEBUG
-// Is FID a <built-in>, <scratch space> etc?
-static bool isSpecialBuffer(FileID FID, const SourceManager &SM) {
- const SrcMgr::FileInfo &FI = SM.getSLocEntry(FID).getFile();
- return FI.getName().startswith("<");
-}
-#endif
-
-llvm::DenseSet<IncludeStructure::HeaderID>
-translateToHeaderIDs(const ReferencedFiles &Files,
- const IncludeStructure &Includes,
- const SourceManager &SM) {
- trace::Span Tracer("IncludeCleaner::translateToHeaderIDs");
- llvm::DenseSet<IncludeStructure::HeaderID> TranslatedHeaderIDs;
- TranslatedHeaderIDs.reserve(Files.User.size());
- for (FileID FID : Files.User) {
- const FileEntry *FE = SM.getFileEntryForID(FID);
- if (!FE) {
- assert(isSpecialBuffer(FID, SM));
- continue;
- }
- const auto File = Includes.getID(FE);
- assert(File);
- TranslatedHeaderIDs.insert(*File);
- }
- for (tooling::stdlib::Header StdlibUsed : Files.Stdlib)
- for (auto HID : Includes.StdlibHeaders.lookup(StdlibUsed))
- TranslatedHeaderIDs.insert(HID);
- return TranslatedHeaderIDs;
+bool match(const include_cleaner::Header &H, const Inclusion &I,
+ const IncludeStructure &S) {
+ switch (H.kind()) {
+ case include_cleaner::Header::Physical:
+ if (auto HID = S.getID(H.getPhysical()))
+ if (static_cast<unsigned>(*HID) == I.HeaderID)
+ return true;
+ break;
+ case include_cleaner::Header::StandardLibrary:
+ return I.Written == H.getStandardLibrary().name();
+ case include_cleaner::Header::Verbatim:
+ return llvm::StringRef(I.Written).trim("\"<>") == H.getVerbatimSpelling();
+ case include_cleaner::Header::Builtin:
+ case include_cleaner::Header::MainFile:
+ break;
+ }
+ return false;
}
std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST) {
- const auto &SM = AST.getSourceManager();
-
- auto Refs = findReferencedLocations(AST);
- auto ReferencedFiles =
- findReferencedFiles(Refs, AST.getIncludeStructure(),
- AST.getCanonicalIncludes(), AST.getSourceManager());
- auto ReferencedHeaders =
- translateToHeaderIDs(ReferencedFiles, AST.getIncludeStructure(), SM);
- return getUnused(AST, ReferencedHeaders, ReferencedFiles.SpelledUmbrellas);
+ include_cleaner::AnalysisContext Ctx(include_cleaner::Policy{},
+ AST.getPreprocessor());
+ llvm::DenseSet<const Inclusion *> Used;
+ include_cleaner::walkUsed(
+ Ctx, AST.getLocalTopLevelDecls(),
+ /*MacroRefs=*/findReferencedMacros(AST, Ctx),
+ [&](SourceLocation Loc, include_cleaner::Symbol Sym,
+ llvm::ArrayRef<include_cleaner::Header> Headers) {
+ for (const auto &I : AST.getIncludeStructure().MainFileIncludes)
+ for (const auto &H : Headers)
+ if (match(H, I, AST.getIncludeStructure()))
+ Used.insert(&I);
+ });
+ std::vector<const Inclusion *> Unused;
+ const Config &Cfg = Config::current();
+ for (const auto &I : AST.getIncludeStructure().MainFileIncludes) {
+ if (!Used.contains(&I) && mayConsiderUnused(I, AST, Cfg))
+ Unused.push_back(&I);
+ }
+ return Unused;
}
-std::vector<Diag> issueUnusedIncludesDiagnostics(ParsedAST &AST,
- llvm::StringRef Code) {
+auto issueUnusedIncludesDiagnostics(ParsedAST &AST,
+ llvm::StringRef Code) -> std::vector<Diag> {
const Config &Cfg = Config::current();
if (Cfg.Diagnostics.UnusedIncludes != Config::UnusedIncludesPolicy::Strict ||
Cfg.Diagnostics.SuppressAll ||
diff --git a/clang-tools-extra/clangd/IncludeCleaner.h b/clang-tools-extra/clangd/IncludeCleaner.h
index 4ce31baaa067..c858a60c5db7 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.h
+++ b/clang-tools-extra/clangd/IncludeCleaner.h
@@ -23,6 +23,7 @@
#include "index/CanonicalIncludes.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/Inclusions/StandardLibrary.h"
+#include "clang-include-cleaner/Types.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringSet.h"
@@ -100,6 +101,10 @@ std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST);
std::vector<Diag> issueUnusedIncludesDiagnostics(ParsedAST &AST,
llvm::StringRef Code);
+// Does an include-cleaner header spec match a clangd recorded inclusion?
+bool match(const include_cleaner::Header &H, const Inclusion &I,
+ const IncludeStructure &S);
+
/// Affects whether standard library includes should be considered for
/// removal. This is off by default for now due to implementation limitations:
/// - macros are not tracked
diff --git a/clang-tools-extra/include-cleaner/CMakeLists.txt b/clang-tools-extra/include-cleaner/CMakeLists.txt
index 0550b02f603b..325186879a47 100644
--- a/clang-tools-extra/include-cleaner/CMakeLists.txt
+++ b/clang-tools-extra/include-cleaner/CMakeLists.txt
@@ -1,4 +1,8 @@
+include_directories(include)
+include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
add_subdirectory(lib)
+add_subdirectory(tool)
+
if(CLANG_INCLUDE_TESTS)
add_subdirectory(test)
add_subdirectory(unittests)
diff --git a/clang-tools-extra/include-cleaner/README.md b/clang-tools-extra/include-cleaner/README.md
deleted file mode 100644
index e69de29bb2d1..000000000000
diff --git a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
new file mode 100644
index 000000000000..4e5cc8d03814
--- /dev/null
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
@@ -0,0 +1,77 @@
+//===--- Analysis.h - Analyze used files --------------------------- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_INCLUDE_CLEANER_ANALYSIS_H
+#define CLANG_INCLUDE_CLEANER_ANALYSIS_H
+
+#include "clang-include-cleaner/Policy.h"
+#include "clang-include-cleaner/Types.h"
+
+namespace clang {
+namespace include_cleaner {
+class Cache;
+
+// Bundles the policy, compiler state, and caches for one include-cleaner run.
+// (This is needed everywhere, but shouldn't be used to propagate state around!)
+class AnalysisContext {
+public:
+ AnalysisContext(const Policy &, const Preprocessor &);
+ AnalysisContext(AnalysisContext &&) = delete;
+ AnalysisContext &operator=(AnalysisContext &&) = delete;
+ ~AnalysisContext();
+
+ const Policy &policy() const { return P; }
+
+ const SourceManager &sourceManager() const { return *SM; }
+ const Preprocessor &preprocessor() const { return *PP; }
+
+ // Only for internal use (the Cache class definition is not exposed).
+ // This allows us to reuse e.g. mappings from symbols to their locations.
+ Cache &cache() { return *C; }
+ // FIXME: does this need to be public?
+ Symbol macro(const IdentifierInfo *, SourceLocation);
+
+private:
+ Policy P;
+ const SourceManager *SM;
+ const Preprocessor *PP;
+ std::unique_ptr<Cache> C;
+};
+
+// A UsedSymbolVisitor is a callback invoked for each symbol reference seen.
+//
+// References occur at a particular location, refer to a single symbol, and
+// that symbol may be provided by any of several headers.
+//
+// The first element of ProvidedBy is the *preferred* header, e.g. to insert.
+using UsedSymbolVisitor =
+ llvm::function_ref<void(SourceLocation UsedAt, Symbol UsedSymbol,
+ llvm::ArrayRef<Header> ProvidedBy)>;
+
+// Find and report all references to symbols in a region of code.
+//
+// The AST traversal is rooted at ASTRoots - typically top-level declarations
+// of a single source file. MacroRefs are additional recorded references to
+// macros, which do not appear in the AST.
+//
+// This is the main entrypoint of the include-cleaner library, and can be used:
+// - to diagnose missing includes: a referenced symbol is provided by
+// headers which don't match any #include in the main file
+// - to diagnose unused includes: an #include in the main file does not match
+// the headers for any referenced symbol
+//
+// Mapping between Header and #include directives is not provided here, but see
+// RecordedPP::Includes::match() in Hooks.h.
+void walkUsed(AnalysisContext &, llvm::ArrayRef<Decl *> ASTRoots,
+ llvm::ArrayRef<SymbolReference> MacroRefs,
+ UsedSymbolVisitor Callback);
+
+} // namespace include_cleaner
+} // namespace clang
+
+#endif
diff --git a/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Hooks.h b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Hooks.h
new file mode 100644
index 000000000000..39e11653b210
--- /dev/null
+++ b/clang-tools-extra/include-cleaner/include/clang-include-cleaner/Hooks.h
@@ -0,0 +1,87 @@
+//===--- Hooks.h - Record compiler events -------------------------- C++-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Where Analysis.h analyzes AST nodes and recorded preprocessor events, this
+// file defines ways to capture AST and preprocessor information from a parse.
+//
+// These are the simplest way to connect include-cleaner logic to the parser,
+// but other ways are possible (for example clangd records includes separately).
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef CLANG_INCLUDE_CLEANER_HOOKS_H
+#define CLANG_INCLUDE_CLEANER_HOOKS_H
+
+#include "Analysis.h"
+#include "Types.h"
+#include "clang/Basic/FileEntry.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/ADT/StringRef.h"
+#include <vector>
+
+namespace clang {
+class FileEntry;
+class PPCallbacks;
+namespace include_cleaner {
+class PPRecorder;
+
+// Contains recorded preprocessor events relevant to include-cleaner.
+struct RecordedPP {
+ // The callback (when installed into clang) tracks macros/includes in this.
+ std::unique_ptr<PPCallbacks> record(AnalysisContext &Ctx);
+ // FIXME: probably also want a comment handler to capture IWYU pragmas.
+
+ // Describes where macros were used from the main file.
+ std::vector<SymbolReference> MacroReferences;
+
+ // A single #include directive from the main file.
+ struct Include {
+ llvm::StringRef Spelled; // e.g. vector
+ const FileEntry *Resolved; // e.g. /path/to/c++/v1/vector
+ SourceLocation Location; // of hash in #include <vector>
+ unsigned Line; // 1-based line number for #include
+ };
+ // The set of includes recorded from the main file.
+ class RecordedIncludes {
+ public:
+ // All #includes seen, in the order they appear.
+ llvm::ArrayRef<Include> all() const { return All; }
+ // Determine #includes that match a header (that provides a used symbol).
+ //
+ // Matching is based on the type of Header specified:
+ // - for a physical file like /path/to/foo.h, we check Resolved
+ // - for a logical file like <vector>, we check Spelled
+ llvm::SmallVector<const Include *> match(Header H) const;
+
+ private:
+ std::vector<Include> All;
+ llvm::StringMap<llvm::SmallVector<unsigned>> BySpelling;
+ llvm::DenseMap<const FileEntry *, llvm::SmallVector<unsigned>> ByFile;
+ friend PPRecorder;
+ } Includes;
+};
+
+// Contains recorded parser events relevant to include-cleaner.
+struct RecordedAST {
+ // The consumer (when installed into clang) tracks declarations in this.
+ std::unique_ptr<ASTConsumer> record(AnalysisContext &Ctx);
+
+ // The set of declarations written at file scope inside the main file.
+ //
+ // These are the roots of the subtrees that should be traversed to find uses.
+ // (Traversing the TranslationUnitDecl would find uses inside headers!)
+ std::vector<Decl *> TopLevelDecls;
+};
+
+} // namespace include_cleaner
+} // namespace clang
+