Skip to content

Commit 41861c7

Browse files
committed
Merge from 'main' to 'sycl-web' (43 commits)
CONFLICT (content): Merge conflict in llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
2 parents 3288267 + 4d33cf4 commit 41861c7

File tree

248 files changed

+5886
-1070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

248 files changed

+5886
-1070
lines changed

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef CLANG_INCLUDE_CLEANER_ANALYSIS_H
1212
#define CLANG_INCLUDE_CLEANER_ANALYSIS_H
1313

14+
#include "clang-include-cleaner/Record.h"
1415
#include "clang-include-cleaner/Types.h"
1516
#include "llvm/ADT/ArrayRef.h"
1617
#include "llvm/ADT/STLFunctionalExtras.h"
@@ -42,11 +43,9 @@ using UsedSymbolCB = llvm::function_ref<void(const SymbolReference &SymRef,
4243
/// headers which don't match any #include in the main file
4344
/// - to diagnose unused includes: an #include in the main file does not match
4445
/// the headers for any referenced symbol
45-
/// FIXME: Take in an include structure to improve location to header mappings
46-
/// (e.g. IWYU pragmas).
4746
void walkUsed(llvm::ArrayRef<Decl *> ASTRoots,
48-
llvm::ArrayRef<SymbolReference> MacroRefs, const SourceManager &,
49-
UsedSymbolCB CB);
47+
llvm::ArrayRef<SymbolReference> MacroRefs,
48+
const PragmaIncludes &PI, const SourceManager &, UsedSymbolCB CB);
5049

5150
} // namespace include_cleaner
5251
} // namespace clang

clang-tools-extra/include-cleaner/include/clang-include-cleaner/Types.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ enum class RefType {
8585
/// Target's use can't be proven, e.g. a candidate for an unresolved overload.
8686
Ambiguous,
8787
};
88+
llvm::raw_ostream &operator<<(llvm::raw_ostream &, RefType);
8889

8990
/// Indicates that a piece of code refers to a symbol.
9091
struct SymbolReference {
@@ -105,10 +106,14 @@ struct Header {
105106
Physical,
106107
/// A recognized standard library header, like <string>.
107108
Standard,
109+
/// A verbatim header spelling, a string quoted with <> or "" that can be
110+
/// #included directly.
111+
Verbatim,
108112
};
109113

110114
Header(const FileEntry *FE) : Storage(FE) {}
111115
Header(tooling::stdlib::Header H) : Storage(H) {}
116+
Header(StringRef VerbatimSpelling) : Storage(VerbatimSpelling) {}
112117

113118
Kind kind() const { return static_cast<Kind>(Storage.index()); }
114119
bool operator==(const Header &RHS) const { return Storage == RHS.Storage; }
@@ -117,11 +122,13 @@ struct Header {
117122
tooling::stdlib::Header standard() const {
118123
return std::get<Standard>(Storage);
119124
}
125+
StringRef verbatim() const {
126+
return std::get<Verbatim>(Storage);
127+
}
120128

121129
private:
122-
// FIXME: Handle verbatim spellings.
123130
// Order must match Kind enum!
124-
std::variant<const FileEntry *, tooling::stdlib::Header> Storage;
131+
std::variant<const FileEntry *, tooling::stdlib::Header, StringRef> Storage;
125132
};
126133
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Header &);
127134

clang-tools-extra/include-cleaner/lib/Analysis.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,47 +13,31 @@
1313
#include "clang/Basic/SourceManager.h"
1414
#include "clang/Tooling/Inclusions/StandardLibrary.h"
1515
#include "llvm/ADT/ArrayRef.h"
16-
#include "llvm/ADT/STLExtras.h"
1716
#include "llvm/ADT/SmallVector.h"
1817

1918
namespace clang::include_cleaner {
20-
namespace {
21-
llvm::SmallVector<Header>
22-
toHeader(llvm::ArrayRef<tooling::stdlib::Header> Headers) {
23-
llvm::SmallVector<Header> Result;
24-
llvm::for_each(Headers, [&](tooling::stdlib::Header H) {
25-
Result.emplace_back(Header(H));
26-
});
27-
return Result;
28-
}
29-
} // namespace
3019

3120
void walkUsed(llvm::ArrayRef<Decl *> ASTRoots,
3221
llvm::ArrayRef<SymbolReference> MacroRefs,
33-
const SourceManager &SM, UsedSymbolCB CB) {
22+
const PragmaIncludes &PI, const SourceManager &SM,
23+
UsedSymbolCB CB) {
3424
tooling::stdlib::Recognizer Recognizer;
3525
for (auto *Root : ASTRoots) {
3626
auto &SM = Root->getASTContext().getSourceManager();
3727
walkAST(*Root, [&](SourceLocation Loc, NamedDecl &ND, RefType RT) {
3828
if (auto SS = Recognizer(&ND)) {
3929
// FIXME: Also report forward decls from main-file, so that the caller
4030
// can decide to insert/ignore a header.
41-
return CB({Loc, Symbol(*SS), RT}, toHeader(SS->headers()));
31+
return CB({Loc, Symbol(*SS), RT}, findHeaders(*SS, SM, PI));
4232
}
4333
// FIXME: Extract locations from redecls.
44-
// FIXME: Handle IWYU pragmas, non self-contained files.
45-
// FIXME: Handle macro locations.
46-
if (auto *FE = SM.getFileEntryForID(SM.getFileID(ND.getLocation())))
47-
return CB({Loc, Symbol(ND), RT}, {Header(FE)});
34+
return CB({Loc, Symbol(ND), RT}, findHeaders(ND.getLocation(), SM, PI));
4835
});
4936
}
5037
for (const SymbolReference &MacroRef : MacroRefs) {
5138
assert(MacroRef.Target.kind() == Symbol::Macro);
52-
// FIXME: Handle IWYU pragmas, non self-contained files.
53-
// FIXME: Handle macro locations.
54-
if (auto *FE = SM.getFileEntryForID(
55-
SM.getFileID(MacroRef.Target.macro().Definition)))
56-
CB(MacroRef, {Header(FE)});
39+
return CB(MacroRef,
40+
findHeaders(MacroRef.Target.macro().Definition, SM, PI));
5741
}
5842
}
5943

clang-tools-extra/include-cleaner/lib/AnalysisInternal.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifndef CLANG_INCLUDE_CLEANER_ANALYSISINTERNAL_H
2222
#define CLANG_INCLUDE_CLEANER_ANALYSISINTERNAL_H
2323

24+
#include "clang-include-cleaner/Record.h"
2425
#include "clang-include-cleaner/Types.h"
2526
#include "clang/Basic/SourceLocation.h"
2627
#include "llvm/ADT/STLFunctionalExtras.h"
@@ -46,6 +47,42 @@ namespace include_cleaner {
4647
void walkAST(Decl &Root,
4748
llvm::function_ref<void(SourceLocation, NamedDecl &, RefType)>);
4849

50+
/// A place where a symbol can be provided.
51+
/// It is either a physical file of the TU (SourceLocation) or a logical
52+
/// location in the standard library (stdlib::Symbol).
53+
struct SymbolLocation {
54+
enum Kind {
55+
/// A position within a source file (or macro expansion) parsed by clang.
56+
Physical,
57+
/// A recognized standard library symbol, like std::string.
58+
Standard,
59+
};
60+
61+
SymbolLocation(SourceLocation S) : Storage(S) {}
62+
SymbolLocation(tooling::stdlib::Symbol S) : Storage(S) {}
63+
64+
Kind kind() const { return static_cast<Kind>(Storage.index()); }
65+
bool operator==(const SymbolLocation &RHS) const {
66+
return Storage == RHS.Storage;
67+
}
68+
69+
SourceLocation physical() const { return std::get<Physical>(Storage); }
70+
tooling::stdlib::Symbol standard() const {
71+
return std::get<Standard>(Storage);
72+
}
73+
74+
private:
75+
// Order must match Kind enum!
76+
std::variant<SourceLocation, tooling::stdlib::Symbol> Storage;
77+
};
78+
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Header &);
79+
80+
/// Finds the headers that provide the symbol location.
81+
// FIXME: expose signals
82+
llvm::SmallVector<Header> findHeaders(const SymbolLocation &Loc,
83+
const SourceManager &SM,
84+
const PragmaIncludes &PI);
85+
4986
/// Write an HTML summary of the analysis to the given stream.
5087
/// FIXME: Once analysis has a public API, this should be public too.
5188
void writeHTMLReport(FileID File, llvm::ArrayRef<Decl *> Roots, ASTContext &Ctx,

clang-tools-extra/include-cleaner/lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ set(LLVM_LINK_COMPONENTS Support)
22

33
add_clang_library(clangIncludeCleaner
44
Analysis.cpp
5+
FindHeaders.cpp
56
HTMLReport.cpp
7+
LocateSymbol.cpp
68
Record.cpp
79
Types.cpp
810
WalkAST.cpp
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===--- FindHeaders.cpp --------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "AnalysisInternal.h"
10+
#include "clang-include-cleaner/Record.h"
11+
#include "clang/Basic/SourceManager.h"
12+
13+
namespace clang::include_cleaner {
14+
15+
llvm::SmallVector<Header> findHeaders(const SymbolLocation &Loc,
16+
const SourceManager &SM,
17+
const PragmaIncludes &PI) {
18+
llvm::SmallVector<Header> Results;
19+
switch (Loc.kind()) {
20+
case SymbolLocation::Physical: {
21+
// FIXME: Handle macro locations.
22+
// FIXME: Handle non self-contained files.
23+
FileID FID = SM.getFileID(Loc.physical());
24+
const auto *FE = SM.getFileEntryForID(FID);
25+
if (!FE)
26+
return {};
27+
28+
// We treat the spelling header in the IWYU pragma as the final public
29+
// header.
30+
// FIXME: look for exporters if the public header is exported by another
31+
// header.
32+
llvm::StringRef VerbatimSpelling = PI.getPublic(FE);
33+
if (!VerbatimSpelling.empty())
34+
return {Header(VerbatimSpelling)};
35+
36+
Results = {Header(FE)};
37+
// FIXME: compute transitive exporter headers.
38+
for (const auto *Export : PI.getExporters(FE, SM.getFileManager()))
39+
Results.push_back(Export);
40+
return Results;
41+
}
42+
case SymbolLocation::Standard: {
43+
for (const auto &H : Loc.standard().headers())
44+
Results.push_back(H);
45+
return Results;
46+
}
47+
}
48+
llvm_unreachable("unhandled SymbolLocation kind!");
49+
}
50+
51+
} // namespace clang::include_cleaner
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===--- LocateSymbol.cpp -------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "AnalysisInternal.h"
10+
#include "llvm/ADT/StringExtras.h"
11+
#include "llvm/Support/raw_ostream.h"
12+
13+
namespace clang::include_cleaner {
14+
15+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolLocation &S) {
16+
switch (S.kind()) {
17+
case SymbolLocation::Physical:
18+
// We can't decode the Location without SourceManager. Its raw
19+
// representation isn't completely useless (and distinguishes
20+
// SymbolReference from Symbol).
21+
return OS << "@0x"
22+
<< llvm::utohexstr(
23+
S.physical().getRawEncoding(), /*LowerCase=*/false,
24+
/*Width=*/CHAR_BIT * sizeof(SourceLocation::UIntTy));
25+
case SymbolLocation::Standard:
26+
return OS << S.standard().scope() << S.standard().name();
27+
}
28+
llvm_unreachable("Unhandled Symbol kind");
29+
}
30+
31+
} // namespace clang::include_cleaner

clang-tools-extra/include-cleaner/lib/Record.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
225225
// 2. handleCommentInMainFile("// IWYU pragma: keep")
226226
// 3. InclusionDirective("bar.h")
227227
//
228-
// This code stores the last location of "IWYU pragma: keep" (or export)
229-
// comment in the main file, so that when next InclusionDirective is
230-
// called, it will know that the next inclusion is behind the IWYU pragma.
228+
// This code stores the last location of "IWYU pragma: keep" comment in
229+
// the main file, so that when next InclusionDirective is called, it will
230+
// know that the next inclusion is behind the IWYU pragma.
231231
LastPragmaKeepInMainFileLine = CommentLine;
232232
}
233233
return false;
@@ -332,6 +332,10 @@ RecordedPP::RecordedIncludes::match(Header H) const {
332332
for (unsigned I : BySpelling.lookup(H.standard().name().trim("<>")))
333333
Result.push_back(&All[I]);
334334
break;
335+
case Header::Verbatim:
336+
for (unsigned I : BySpelling.lookup(H.verbatim().trim("\"<>")))
337+
Result.push_back(&All[I]);
338+
break;
335339
}
336340
return Result;
337341
}

clang-tools-extra/include-cleaner/lib/Types.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) {
3434
return OS << H.physical()->getName();
3535
case Header::Standard:
3636
return OS << H.standard().name();
37+
case Header::Verbatim:
38+
return OS << H.verbatim();
3739
}
3840
llvm_unreachable("Unhandled Header kind");
3941
}
@@ -52,4 +54,16 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolReference &R) {
5254
/*Width=*/CHAR_BIT * sizeof(SourceLocation::UIntTy));
5355
}
5456

57+
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, RefType T) {
58+
switch (T) {
59+
case RefType::Explicit:
60+
return OS << "explicit";
61+
case RefType::Implicit:
62+
return OS << "implicit";
63+
case RefType::Ambiguous:
64+
return OS << "ambiguous";
65+
}
66+
llvm_unreachable("Unexpected RefType");
67+
}
68+
5569
} // namespace clang::include_cleaner

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AnalysisInternal.h"
10-
#include "clang-include-cleaner/Analysis.h"
1110
#include "clang/AST/Decl.h"
1211
#include "clang/AST/DeclCXX.h"
1312
#include "clang/AST/Expr.h"

0 commit comments

Comments
 (0)