Skip to content

Commit c04153d

Browse files
committed
Merge from '"main"' to '"sycl-web"' (1 commits)
CONFLICT (content): Merge conflict in README.md
2 parents 3c3b127 + 02c0183 commit c04153d

File tree

1,062 files changed

+53687
-17732
lines changed

Some content is hidden

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

1,062 files changed

+53687
-17732
lines changed

clang-tools-extra/clang-tidy/add_new_check.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ def update_checks_list(clang_tidy_path):
323323
def has_auto_fix(check_name):
324324
dirname, _, check_name = check_name.partition("-")
325325

326-
checkerCode = get_actual_filename(dirname,
327-
get_camel_name(check_name) + '.cpp')
326+
checker_code = get_actual_filename(os.path.join(clang_tidy_path, dirname),
327+
get_camel_name(check_name) + '.cpp')
328328

329-
if not os.path.isfile(checkerCode):
329+
if not os.path.isfile(checker_code):
330330
return ""
331331

332-
with io.open(checkerCode, encoding='utf8') as f:
332+
with io.open(checker_code, encoding='utf8') as f:
333333
code = f.read()
334334
if 'FixItHint' in code or "ReplacementText" in code or "fixit" in code:
335335
# Some simple heuristics to figure out if a checker has an autofix or not.

clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
165165
Expr::EvalResult ConstPtr;
166166
if (!Ptr->isInstantiationDependent() &&
167167
Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
168-
((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isNullValue()) ||
168+
((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isZero()) ||
169169
(ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
170170
diag(Loc, "constructing string from nullptr is undefined behaviour");
171171
}

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ bool semaCodeComplete(std::unique_ptr<CodeCompleteConsumer> Consumer,
12031203
loadMainFilePreambleMacros(Clang->getPreprocessor(), Input.Preamble);
12041204
if (Includes)
12051205
Clang->getPreprocessor().addPPCallbacks(
1206-
collectIncludeStructureCallback(Clang->getSourceManager(), Includes));
1206+
Includes->collect(Clang->getSourceManager()));
12071207
if (llvm::Error Err = Action.Execute()) {
12081208
log("Execute() failed when running codeComplete for {0}: {1}",
12091209
Input.FileName, toString(std::move(Err)));
@@ -1380,7 +1380,7 @@ class CodeCompleteFlow {
13801380
const auto &SM = Recorder->CCSema->getSourceManager();
13811381
llvm::StringMap<SourceParams> ProxSources;
13821382
auto MainFileID =
1383-
Includes.getID(SM.getFileEntryForID(SM.getMainFileID()), SM);
1383+
Includes.getID(SM.getFileEntryForID(SM.getMainFileID()));
13841384
assert(MainFileID);
13851385
for (auto &HeaderIDAndDepth : Includes.includeDepth(*MainFileID)) {
13861386
auto &Source =

clang-tools-extra/clangd/Headers.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class RecordHeaders : public PPCallbacks {
5656
SM.getLineNumber(SM.getFileID(HashLoc), Inc.HashOffset) - 1;
5757
Inc.FileKind = FileKind;
5858
Inc.Directive = IncludeTok.getIdentifierInfo()->getPPKeywordID();
59+
if (File)
60+
Inc.HeaderID = static_cast<unsigned>(Out->getOrCreateID(File));
5961
}
6062

6163
// Record include graph (not just for main-file includes)
@@ -67,8 +69,8 @@ class RecordHeaders : public PPCallbacks {
6769
// Treat as if included from the main file.
6870
IncludingFileEntry = SM.getFileEntryForID(MainFID);
6971
}
70-
auto IncludingID = Out->getOrCreateID(IncludingFileEntry, SM),
71-
IncludedID = Out->getOrCreateID(File, SM);
72+
auto IncludingID = Out->getOrCreateID(IncludingFileEntry),
73+
IncludedID = Out->getOrCreateID(File);
7274
Out->IncludeChildren[IncludingID].push_back(IncludedID);
7375
}
7476
}
@@ -150,16 +152,15 @@ llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) {
150152
}
151153

152154
std::unique_ptr<PPCallbacks>
153-
collectIncludeStructureCallback(const SourceManager &SM,
154-
IncludeStructure *Out) {
155-
return std::make_unique<RecordHeaders>(SM, Out);
155+
IncludeStructure::collect(const SourceManager &SM) {
156+
MainFileEntry = SM.getFileEntryForID(SM.getMainFileID());
157+
return std::make_unique<RecordHeaders>(SM, this);
156158
}
157159

158160
llvm::Optional<IncludeStructure::HeaderID>
159-
IncludeStructure::getID(const FileEntry *Entry,
160-
const SourceManager &SM) const {
161+
IncludeStructure::getID(const FileEntry *Entry) const {
161162
// HeaderID of the main file is always 0;
162-
if (SM.getMainFileID() == SM.translateFile(Entry)) {
163+
if (Entry == MainFileEntry) {
163164
return static_cast<IncludeStructure::HeaderID>(0u);
164165
}
165166
auto It = UIDToIndex.find(Entry->getUniqueID());
@@ -169,12 +170,12 @@ IncludeStructure::getID(const FileEntry *Entry,
169170
}
170171

171172
IncludeStructure::HeaderID
172-
IncludeStructure::getOrCreateID(const FileEntry *Entry,
173-
const SourceManager &SM) {
174-
// Main file's FileID was not known at IncludeStructure creation time.
175-
if (SM.getMainFileID() == SM.translateFile(Entry)) {
176-
UIDToIndex[Entry->getUniqueID()] =
177-
static_cast<IncludeStructure::HeaderID>(0u);
173+
IncludeStructure::getOrCreateID(const FileEntry *Entry) {
174+
// Main file's FileEntry was not known at IncludeStructure creation time.
175+
if (Entry == MainFileEntry) {
176+
if (RealPathNames.front().empty())
177+
RealPathNames.front() = MainFileEntry->tryGetRealPathName().str();
178+
return MainFileID;
178179
}
179180
auto R = UIDToIndex.try_emplace(
180181
Entry->getUniqueID(),

clang-tools-extra/clangd/Headers.h

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "index/Symbol.h"
1515
#include "support/Logger.h"
1616
#include "support/Path.h"
17+
#include "clang/Basic/FileEntry.h"
1718
#include "clang/Basic/TokenKinds.h"
1819
#include "clang/Format/Format.h"
1920
#include "clang/Lex/HeaderSearch.h"
@@ -60,6 +61,7 @@ struct Inclusion {
6061
unsigned HashOffset = 0; // Byte offset from start of file to #.
6162
int HashLine = 0; // Line number containing the directive, 0-indexed.
6263
SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
64+
llvm::Optional<unsigned> HeaderID;
6365
};
6466
llvm::raw_ostream &operator<<(llvm::raw_ostream &, const Inclusion &);
6567
bool operator==(const Inclusion &LHS, const Inclusion &RHS);
@@ -119,15 +121,22 @@ class IncludeStructure {
119121
RealPathNames.emplace_back();
120122
}
121123

124+
// Returns a PPCallback that visits all inclusions in the main file and
125+
// populates the structure.
126+
std::unique_ptr<PPCallbacks> collect(const SourceManager &SM);
127+
128+
void setMainFileEntry(const FileEntry *Entry) {
129+
assert(Entry && Entry->isValid());
130+
this->MainFileEntry = Entry;
131+
}
132+
122133
// HeaderID identifies file in the include graph. It corresponds to a
123134
// FileEntry rather than a FileID, but stays stable across preamble & main
124135
// file builds.
125136
enum class HeaderID : unsigned {};
126137

127-
llvm::Optional<HeaderID> getID(const FileEntry *Entry,
128-
const SourceManager &SM) const;
129-
HeaderID getOrCreateID(const FileEntry *Entry,
130-
const SourceManager &SM);
138+
llvm::Optional<HeaderID> getID(const FileEntry *Entry) const;
139+
HeaderID getOrCreateID(const FileEntry *Entry);
131140

132141
StringRef getRealPath(HeaderID ID) const {
133142
assert(static_cast<unsigned>(ID) <= RealPathNames.size());
@@ -141,32 +150,33 @@ class IncludeStructure {
141150
// All transitive includes (absolute paths), with their minimum include depth.
142151
// Root --> 0, #included file --> 1, etc.
143152
// Root is the ID of the header being visited first.
144-
// Usually it is getID(SM.getFileEntryForID(SM.getMainFileID()), SM).
145-
llvm::DenseMap<HeaderID, unsigned> includeDepth(HeaderID Root) const;
153+
llvm::DenseMap<HeaderID, unsigned>
154+
includeDepth(HeaderID Root = MainFileID) const;
146155

147156
// Maps HeaderID to the ids of the files included from it.
148157
llvm::DenseMap<HeaderID, SmallVector<HeaderID>> IncludeChildren;
149158

150159
std::vector<Inclusion> MainFileIncludes;
151160

161+
// We reserve HeaderID(0) for the main file and will manually check for that
162+
// in getID and getOrCreateID because the UniqueID is not stable when the
163+
// content of the main file changes.
164+
static const HeaderID MainFileID = HeaderID(0u);
165+
152166
private:
167+
// MainFileEntry will be used to check if the queried file is the main file
168+
// or not.
169+
const FileEntry *MainFileEntry = nullptr;
170+
153171
std::vector<std::string> RealPathNames; // In HeaderID order.
154-
// HeaderID maps the FileEntry::UniqueID to the internal representation.
172+
// FileEntry::UniqueID is mapped to the internal representation (HeaderID).
155173
// Identifying files in a way that persists from preamble build to subsequent
156-
// builds is surprisingly hard. FileID is unavailable in
157-
// InclusionDirective(), and RealPathName and UniqueID are not preserved in
174+
// builds is surprisingly hard. FileID is unavailable in InclusionDirective(),
175+
// and RealPathName and UniqueID are not preserved in
158176
// the preamble.
159-
//
160-
// We reserve 0 to the main file and will manually check for that in getID
161-
// and getOrCreateID because llvm::sys::fs::UniqueID is not stable when their
162-
// content of the main file changes.
163177
llvm::DenseMap<llvm::sys::fs::UniqueID, HeaderID> UIDToIndex;
164178
};
165179

166-
/// Returns a PPCallback that visits all inclusions in the main file.
167-
std::unique_ptr<PPCallbacks>
168-
collectIncludeStructureCallback(const SourceManager &SM, IncludeStructure *Out);
169-
170180
// Calculates insertion edit for including a new header in a file.
171181
class IncludeInserter {
172182
public:
@@ -228,7 +238,7 @@ class IncludeInserter {
228238

229239
namespace llvm {
230240

231-
// Support Tokens as DenseMap keys.
241+
// Support HeaderIDs as DenseMap keys.
232242
template <> struct DenseMapInfo<clang::clangd::IncludeStructure::HeaderID> {
233243
static inline clang::clangd::IncludeStructure::HeaderID getEmptyKey() {
234244
return static_cast<clang::clangd::IncludeStructure::HeaderID>(
@@ -251,30 +261,6 @@ template <> struct DenseMapInfo<clang::clangd::IncludeStructure::HeaderID> {
251261
}
252262
};
253263

254-
// Support Tokens as DenseMap keys.
255-
template <> struct DenseMapInfo<llvm::sys::fs::UniqueID> {
256-
static inline llvm::sys::fs::UniqueID getEmptyKey() {
257-
auto EmptyKey = DenseMapInfo<std::pair<unsigned, unsigned>>::getEmptyKey();
258-
return {EmptyKey.first, EmptyKey.second};
259-
}
260-
261-
static inline llvm::sys::fs::UniqueID getTombstoneKey() {
262-
auto TombstoneKey =
263-
DenseMapInfo<std::pair<unsigned, unsigned>>::getTombstoneKey();
264-
return {TombstoneKey.first, TombstoneKey.second};
265-
}
266-
267-
static unsigned getHashValue(const llvm::sys::fs::UniqueID &Tag) {
268-
return hash_value(
269-
std::pair<unsigned, unsigned>(Tag.getDevice(), Tag.getFile()));
270-
}
271-
272-
static bool isEqual(const llvm::sys::fs::UniqueID &LHS,
273-
const llvm::sys::fs::UniqueID &RHS) {
274-
return LHS == RHS;
275-
}
276-
};
277-
278264
} // namespace llvm
279265

280266
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_HEADERS_H

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,35 @@ class ReferencedLocationCrawler
9898
llvm::DenseSet<const void *> Visited;
9999
};
100100

101+
// Given a set of referenced FileIDs, determines all the potentially-referenced
102+
// files and macros by traversing expansion/spelling locations of macro IDs.
103+
// This is used to map the referenced SourceLocations onto real files.
104+
struct ReferencedFiles {
105+
ReferencedFiles(const SourceManager &SM) : SM(SM) {}
106+
llvm::DenseSet<FileID> Files;
107+
llvm::DenseSet<FileID> Macros;
108+
const SourceManager &SM;
109+
110+
void add(SourceLocation Loc) { add(SM.getFileID(Loc), Loc); }
111+
112+
void add(FileID FID, SourceLocation Loc) {
113+
if (FID.isInvalid())
114+
return;
115+
assert(SM.isInFileID(Loc, FID));
116+
if (Loc.isFileID()) {
117+
Files.insert(FID);
118+
return;
119+
}
120+
// Don't process the same macro FID twice.
121+
if (!Macros.insert(FID).second)
122+
return;
123+
const auto &Exp = SM.getSLocEntry(FID).getExpansion();
124+
add(Exp.getSpellingLoc());
125+
add(Exp.getExpansionLocStart());
126+
add(Exp.getExpansionLocEnd());
127+
}
128+
};
129+
101130
} // namespace
102131

103132
ReferencedLocations findReferencedLocations(ParsedAST &AST) {
@@ -108,5 +137,65 @@ ReferencedLocations findReferencedLocations(ParsedAST &AST) {
108137
return Result;
109138
}
110139

140+
llvm::DenseSet<FileID>
141+
findReferencedFiles(const llvm::DenseSet<SourceLocation> &Locs,
142+
const SourceManager &SM) {
143+
std::vector<SourceLocation> Sorted{Locs.begin(), Locs.end()};
144+
llvm::sort(Sorted); // Group by FileID.
145+
ReferencedFiles Result(SM);
146+
for (auto It = Sorted.begin(); It < Sorted.end();) {
147+
FileID FID = SM.getFileID(*It);
148+
Result.add(FID, *It);
149+
// Cheaply skip over all the other locations from the same FileID.
150+
// This avoids lots of redundant Loc->File lookups for the same file.
151+
do
152+
++It;
153+
while (It != Sorted.end() && SM.isInFileID(*It, FID));
154+
}
155+
return std::move(Result.Files);
156+
}
157+
158+
std::vector<const Inclusion *>
159+
getUnused(const IncludeStructure &Structure,
160+
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles) {
161+
std::vector<const Inclusion *> Unused;
162+
for (const Inclusion &MFI : Structure.MainFileIncludes) {
163+
// FIXME: Skip includes that are not self-contained.
164+
assert(MFI.HeaderID);
165+
auto IncludeID = static_cast<IncludeStructure::HeaderID>(*MFI.HeaderID);
166+
if (!ReferencedFiles.contains(IncludeID)) {
167+
Unused.push_back(&MFI);
168+
}
169+
dlog("{0} is {1}", MFI.Written,
170+
ReferencedFiles.contains(IncludeID) ? "USED" : "UNUSED");
171+
}
172+
return Unused;
173+
}
174+
175+
llvm::DenseSet<IncludeStructure::HeaderID>
176+
translateToHeaderIDs(const llvm::DenseSet<FileID> &Files,
177+
const IncludeStructure &Includes,
178+
const SourceManager &SM) {
179+
llvm::DenseSet<IncludeStructure::HeaderID> TranslatedHeaderIDs;
180+
TranslatedHeaderIDs.reserve(Files.size());
181+
for (FileID FID : Files) {
182+
const FileEntry *FE = SM.getFileEntryForID(FID);
183+
assert(FE);
184+
const auto File = Includes.getID(FE);
185+
assert(File);
186+
TranslatedHeaderIDs.insert(*File);
187+
}
188+
return TranslatedHeaderIDs;
189+
}
190+
191+
std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST) {
192+
const auto &SM = AST.getSourceManager();
193+
194+
auto Refs = findReferencedLocations(AST);
195+
auto ReferencedFiles = translateToHeaderIDs(findReferencedFiles(Refs, SM),
196+
AST.getIncludeStructure(), SM);
197+
return getUnused(AST.getIncludeStructure(), ReferencedFiles);
198+
}
199+
111200
} // namespace clangd
112201
} // namespace clang

clang-tools-extra/clangd/IncludeCleaner.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "ParsedAST.h"
2626
#include "clang/Basic/SourceLocation.h"
2727
#include "llvm/ADT/DenseSet.h"
28+
#include <vector>
2829

2930
namespace clang {
3031
namespace clangd {
@@ -46,6 +47,22 @@ using ReferencedLocations = llvm::DenseSet<SourceLocation>;
4647
/// - err on the side of reporting all possible locations
4748
ReferencedLocations findReferencedLocations(ParsedAST &AST);
4849

50+
/// Retrieves IDs of all files containing SourceLocations from \p Locs.
51+
llvm::DenseSet<FileID> findReferencedFiles(const ReferencedLocations &Locs,
52+
const SourceManager &SM);
53+
54+
/// Maps FileIDs to the internal IncludeStructure representation (HeaderIDs).
55+
llvm::DenseSet<IncludeStructure::HeaderID>
56+
translateToHeaderIDs(const llvm::DenseSet<FileID> &Files,
57+
const IncludeStructure &Includes, const SourceManager &SM);
58+
59+
/// Retrieves headers that are referenced from the main file but not used.
60+
std::vector<const Inclusion *>
61+
getUnused(const IncludeStructure &Includes,
62+
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles);
63+
64+
std::vector<const Inclusion *> computeUnusedIncludes(ParsedAST &AST);
65+
4966
} // namespace clangd
5067
} // namespace clang
5168

clang-tools-extra/clangd/ParsedAST.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
439439
// Otherwise we would collect the replayed includes again...
440440
// (We can't *just* use the replayed includes, they don't have Resolved path).
441441
Clang->getPreprocessor().addPPCallbacks(
442-
collectIncludeStructureCallback(Clang->getSourceManager(), &Includes));
442+
Includes.collect(Clang->getSourceManager()));
443443
// Copy over the macros in the preamble region of the main file, and combine
444444
// with non-preamble macros below.
445445
MainFileMacros Macros;
@@ -624,5 +624,6 @@ llvm::Optional<llvm::StringRef> ParsedAST::preambleVersion() const {
624624
return llvm::None;
625625
return llvm::StringRef(Preamble->Version);
626626
}
627+
627628
} // namespace clangd
628629
} // namespace clang

0 commit comments

Comments
 (0)