| 1 |
//===--- Record.cpp - Record compiler events ------------------------------===// |
| 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 "clang-include-cleaner/Record.h" |
| 10 |
#include "clang-include-cleaner/Types.h" |
| 11 |
#include "clang/AST/ASTConsumer.h" |
| 12 |
#include "clang/AST/ASTContext.h" |
| 13 |
#include "clang/AST/DeclGroup.h" |
| 14 |
#include "clang/Basic/SourceManager.h" |
| 15 |
#include "clang/Basic/Specifiers.h" |
| 16 |
#include "clang/Frontend/CompilerInstance.h" |
| 17 |
#include "clang/Lex/MacroInfo.h" |
| 18 |
#include "clang/Lex/PPCallbacks.h" |
| 19 |
#include "clang/Lex/Preprocessor.h" |
| 20 |
#include "clang/Tooling/Inclusions/HeaderAnalysis.h" |
| 21 |
#include "clang/Tooling/Inclusions/StandardLibrary.h" |
| 22 |
|
| 23 |
namespace clang::include_cleaner { |
| 24 |
namespace { |
| 25 |
|
| 26 |
class PPRecorder : public PPCallbacks { |
| 27 |
public: |
| 28 |
PPRecorder(RecordedPP &Recorded, const Preprocessor &PP) |
| 29 |
: Recorded(Recorded), PP(PP), SM(PP.getSourceManager()) {} |
| 30 |
|
| 31 |
void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 32 |
SrcMgr::CharacteristicKind FileType, |
| 33 |
FileID PrevFID) override { |
| 34 |
Active = SM.isWrittenInMainFile(Loc); |
| 35 |
} |
| 36 |
|
| 37 |
void InclusionDirective(SourceLocation Hash, const Token &IncludeTok, |
| 38 |
StringRef SpelledFilename, bool IsAngled, |
| 39 |
CharSourceRange FilenameRange, |
| 40 |
OptionalFileEntryRef File, StringRef SearchPath, |
| 41 |
StringRef RelativePath, const Module *, |
| 42 |
SrcMgr::CharacteristicKind) override { |
| 43 |
if (!Active) |
| 44 |
return; |
| 45 |
|
| 46 |
Include I; |
| 47 |
I.HashLocation = Hash; |
| 48 |
I.Resolved = File ? &File->getFileEntry() : nullptr; |
| 49 |
I.Line = SM.getSpellingLineNumber(Hash); |
| 50 |
I.Spelled = SpelledFilename; |
| 51 |
I.Angled = IsAngled; |
| 52 |
Recorded.Includes.add(I); |
| 53 |
} |
| 54 |
|
| 55 |
void MacroExpands(const Token &MacroName, const MacroDefinition &MD, |
| 56 |
SourceRange Range, const MacroArgs *Args) override { |
| 57 |
if (!Active) |
| 58 |
return; |
| 59 |
recordMacroRef(MacroName, *MD.getMacroInfo()); |
| 60 |
} |
| 61 |
|
| 62 |
void MacroDefined(const Token &MacroName, const MacroDirective *MD) override { |
| 63 |
if (!Active) |
| 64 |
return; |
| 65 |
|
| 66 |
const auto *MI = MD->getMacroInfo(); |
| 67 |
// The tokens of a macro definition could refer to a macro. |
| 68 |
// Formally this reference isn't resolved until this macro is expanded, |
| 69 |
// but we want to treat it as a reference anyway. |
| 70 |
for (const auto &Tok : MI->tokens()) { |
| 71 |
auto *II = Tok.getIdentifierInfo(); |
| 72 |
// Could this token be a reference to a macro? (Not param to this macro). |
| 73 |
if (!II || !II->hadMacroDefinition() || |
| 74 |
llvm::is_contained(MI->params(), II)) |
| 75 |
continue; |
| 76 |
if (const MacroInfo *MI = PP.getMacroInfo(II)) |
| 77 |
recordMacroRef(Tok, *MI); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void MacroUndefined(const Token &MacroName, const MacroDefinition &MD, |
| 82 |
const MacroDirective *) override { |
| 83 |
if (!Active) |
| 84 |
return; |
| 85 |
if (const auto *MI = MD.getMacroInfo()) |
| 86 |
recordMacroRef(MacroName, *MI); |
| 87 |
} |
| 88 |
|
| 89 |
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, |
| 90 |
const MacroDefinition &MD) override { |
| 91 |
if (!Active) |
| 92 |
return; |
| 93 |
if (const auto *MI = MD.getMacroInfo()) |
| 94 |
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); |
| 95 |
} |
| 96 |
|
| 97 |
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, |
| 98 |
const MacroDefinition &MD) override { |
| 99 |
if (!Active) |
| 100 |
return; |
| 101 |
if (const auto *MI = MD.getMacroInfo()) |
| 102 |
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); |
| 103 |
} |
| 104 |
|
| 105 |
using PPCallbacks::Elifdef; |
| 106 |
using PPCallbacks::Elifndef; |
| 107 |
void Elifdef(SourceLocation Loc, const Token &MacroNameTok, |
| 108 |
const MacroDefinition &MD) override { |
| 109 |
if (!Active) |
| 110 |
return; |
| 111 |
if (const auto *MI = MD.getMacroInfo()) |
| 112 |
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); |
| 113 |
} |
| 114 |
void Elifndef(SourceLocation Loc, const Token &MacroNameTok, |
| 115 |
const MacroDefinition &MD) override { |
| 116 |
if (!Active) |
| 117 |
return; |
| 118 |
if (const auto *MI = MD.getMacroInfo()) |
| 119 |
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); |
| 120 |
} |
| 121 |
|
| 122 |
void Defined(const Token &MacroNameTok, const MacroDefinition &MD, |
| 123 |
SourceRange Range) override { |
| 124 |
if (!Active) |
| 125 |
return; |
| 126 |
if (const auto *MI = MD.getMacroInfo()) |
| 127 |
recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); |
| 128 |
} |
| 129 |
|
| 130 |
private: |
| 131 |
void recordMacroRef(const Token &Tok, const MacroInfo &MI, |
| 132 |
RefType RT = RefType::Explicit) { |
| 133 |
if (MI.isBuiltinMacro()) |
| 134 |
return; // __FILE__ is not a reference. |
| 135 |
Recorded.MacroReferences.push_back(SymbolReference{ |
| 136 |
Tok.getLocation(), |
| 137 |
Macro{Tok.getIdentifierInfo(), MI.getDefinitionLoc()}, RT}); |
| 138 |
} |
| 139 |
|
| 140 |
bool Active = false; |
| 141 |
RecordedPP &Recorded; |
| 142 |
const Preprocessor &PP; |
| 143 |
const SourceManager &SM; |
| 144 |
}; |
| 145 |
|
| 146 |
} // namespace |
| 147 |
|
| 148 |
class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler { |
| 149 |
public: |
| 150 |
RecordPragma(const CompilerInstance &CI, PragmaIncludes *Out) |
| 151 |
: SM(CI.getSourceManager()), |
| 152 |
HeaderInfo(CI.getPreprocessor().getHeaderSearchInfo()), Out(Out), |
| 153 |
UniqueStrings(Arena) {} |
| 154 |
|
| 155 |
void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 156 |
SrcMgr::CharacteristicKind FileType, |
| 157 |
FileID PrevFID) override { |
| 158 |
InMainFile = SM.isWrittenInMainFile(Loc); |
| 159 |
|
| 160 |
if (Reason == PPCallbacks::ExitFile) { |
| 161 |
// At file exit time HeaderSearchInfo is valid and can be used to |
| 162 |
// determine whether the file was a self-contained header or not. |
| 163 |
if (const FileEntry *FE = SM.getFileEntryForID(PrevFID)) { |
| 164 |
if (tooling::isSelfContainedHeader(FE, SM, HeaderInfo)) |
| 165 |
Out->NonSelfContainedFiles.erase(FE->getUniqueID()); |
| 166 |
else |
| 167 |
Out->NonSelfContainedFiles.insert(FE->getUniqueID()); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
void EndOfMainFile() override { |
| 173 |
for (auto &It : Out->IWYUExportBy) { |
| 174 |
llvm::sort(It.getSecond()); |
| 175 |
It.getSecond().erase( |
| 176 |
std::unique(It.getSecond().begin(), It.getSecond().end()), |
| 177 |
It.getSecond().end()); |
| 178 |
} |
| 179 |
Out->Arena = std::move(Arena); |
| 180 |
} |
| 181 |
|
| 182 |
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 183 |
llvm::StringRef FileName, bool IsAngled, |
| 184 |
CharSourceRange /*FilenameRange*/, |
| 185 |
OptionalFileEntryRef File, |
| 186 |
llvm::StringRef /*SearchPath*/, |
| 187 |
llvm::StringRef /*RelativePath*/, |
| 188 |
const clang::Module * /*Imported*/, |
| 189 |
SrcMgr::CharacteristicKind FileKind) override { |
| 190 |
FileID HashFID = SM.getFileID(HashLoc); |
| 191 |
int HashLine = SM.getLineNumber(HashFID, SM.getFileOffset(HashLoc)); |
| 192 |
std::optional<Header> IncludedHeader; |
| 193 |
if (IsAngled) |
| 194 |
if (auto StandardHeader = |
| 195 |
tooling::stdlib::Header::named("<" + FileName.str() + ">")) { |
| 196 |
IncludedHeader = *StandardHeader; |
| 197 |
} |
| 198 |
if (!IncludedHeader && File) |
| 199 |
IncludedHeader = &File->getFileEntry(); |
| 200 |
checkForExport(HashFID, HashLine, std::move(IncludedHeader)); |
| 201 |
checkForKeep(HashLine); |
| 202 |
} |
| 203 |
|
| 204 |
void checkForExport(FileID IncludingFile, int HashLine, |
| 205 |
std::optional<Header> IncludedHeader) { |
| 206 |
if (ExportStack.empty()) |
| 207 |
return; |
| 208 |
auto &Top = ExportStack.back(); |
| 209 |
if (Top.SeenAtFile != IncludingFile) |
| 210 |
return; |
| 211 |
// Make sure current include is covered by the export pragma. |
| 212 |
if ((Top.Block && HashLine > Top.SeenAtLine) || |
| 213 |
Top.SeenAtLine == HashLine) { |
| 214 |
if (IncludedHeader) { |
| 215 |
switch (IncludedHeader->kind()) { |
| 216 |
case Header::Physical: |
| 217 |
Out->IWYUExportBy[IncludedHeader->physical()->getUniqueID()] |
| 218 |
.push_back(Top.Path); |
| 219 |
break; |
| 220 |
case Header::Standard: |
| 221 |
Out->StdIWYUExportBy[IncludedHeader->standard()].push_back(Top.Path); |
| 222 |
break; |
| 223 |
case Header::Verbatim: |
| 224 |
assert(false && "unexpected Verbatim header"); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
// main-file #include with export pragma should never be removed. |
| 229 |
if (Top.SeenAtFile == SM.getMainFileID()) |
| 230 |
Out->ShouldKeep.insert(HashLine); |
| 231 |
} |
| 232 |
if (!Top.Block) // Pop immediately for single-line export pragma. |
| 233 |
ExportStack.pop_back(); |
| 234 |
} |
| 235 |
|
| 236 |
void checkForKeep(int HashLine) { |
| 237 |
if (!InMainFile || KeepStack.empty()) |
| 238 |
return; |
| 239 |
KeepPragma &Top = KeepStack.back(); |
| 240 |
// Check if the current include is covered by a keep pragma. |
| 241 |
if ((Top.Block && HashLine > Top.SeenAtLine) || Top.SeenAtLine == HashLine) |
| 242 |
Out->ShouldKeep.insert(HashLine); |
| 243 |
|
| 244 |
if (!Top.Block) |
| 245 |
KeepStack.pop_back(); // Pop immediately for single-line keep pragma. |
| 246 |
} |
| 247 |
|
| 248 |
bool HandleComment(Preprocessor &PP, SourceRange Range) override { |
| 249 |
auto &SM = PP.getSourceManager(); |
| 250 |
auto Pragma = |
| 251 |
tooling::parseIWYUPragma(SM.getCharacterData(Range.getBegin())); |
| 252 |
if (!Pragma) |
| 253 |
return false; |
| 254 |
|
| 255 |
if (Pragma->consume_front("private")) { |
| 256 |
auto *FE = SM.getFileEntryForID(SM.getFileID(Range.getBegin())); |
| 257 |
if (!FE) |
| 258 |
return false; |
| 259 |
StringRef PublicHeader; |
| 260 |
if (Pragma->consume_front(", include ")) { |
| 261 |
// We always insert using the spelling from the pragma. |
| 262 |
PublicHeader = save(Pragma->startswith("<") || Pragma->startswith("\"") |
| 263 |
? (*Pragma) |
| 264 |
: ("\"" + *Pragma + "\"").str()); |
| 265 |
} |
| 266 |
Out->IWYUPublic.insert({FE->getLastRef().getUniqueID(), PublicHeader}); |
| 267 |
return false; |
| 268 |
} |
| 269 |
FileID CommentFID = SM.getFileID(Range.getBegin()); |
| 270 |
int CommentLine = SM.getLineNumber(SM.getFileID(Range.getBegin()), |
| 271 |
SM.getFileOffset(Range.getBegin())); |
| 272 |
// Record export pragma. |
| 273 |
if (Pragma->startswith("export")) { |
| 274 |
ExportStack.push_back({CommentLine, CommentFID, |
| 275 |
save(SM.getFileEntryForID(CommentFID)->getName()), |
| 276 |
false}); |
| 277 |
} else if (Pragma->startswith("begin_exports")) { |
| 278 |
ExportStack.push_back({CommentLine, CommentFID, |
| 279 |
save(SM.getFileEntryForID(CommentFID)->getName()), |
| 280 |
true}); |
| 281 |
} else if (Pragma->startswith("end_exports")) { |
| 282 |
// FIXME: be robust on unmatching cases. We should only pop the stack if |
| 283 |
// the begin_exports and end_exports is in the same file. |
| 284 |
if (!ExportStack.empty()) { |
| 285 |
assert(ExportStack.back().Block); |
| 286 |
ExportStack.pop_back(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (InMainFile) { |
| 291 |
if (Pragma->startswith("keep")) { |
| 292 |
KeepStack.push_back({CommentLine, false}); |
| 293 |
} else if (Pragma->starts_with("begin_keep")) { |
| 294 |
KeepStack.push_back({CommentLine, true}); |
| 295 |
} else if (Pragma->starts_with("end_keep") && !KeepStack.empty()) { |
| 296 |
assert(KeepStack.back().Block); |
| 297 |
KeepStack.pop_back(); |
| 298 |
} |
| 299 |
} |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
private: |
| 304 |
StringRef save(llvm::StringRef S) { return UniqueStrings.save(S); } |
| 305 |
|
| 306 |
bool InMainFile = false; |
| 307 |
const SourceManager &SM; |
| 308 |
HeaderSearch &HeaderInfo; |
| 309 |
PragmaIncludes *Out; |
| 310 |
llvm::BumpPtrAllocator Arena; |
| 311 |
/// Intern table for strings. Contents are on the arena. |
| 312 |
llvm::StringSaver UniqueStrings; |
| 313 |
|
| 314 |
struct ExportPragma { |
| 315 |
// The line number where we saw the begin_exports or export pragma. |
| 316 |
int SeenAtLine = 0; // 1-based line number. |
| 317 |
// The file where we saw the pragma. |
| 318 |
FileID SeenAtFile; |
| 319 |
// Name (per FileEntry::getName()) of the file SeenAtFile. |
| 320 |
StringRef Path; |
| 321 |
// true if it is a block begin/end_exports pragma; false if it is a |
| 322 |
// single-line export pragma. |
| 323 |
bool Block = false; |
| 324 |
}; |
| 325 |
// A stack for tracking all open begin_exports or single-line export. |
| 326 |
std::vector<ExportPragma> ExportStack; |
| 327 |
|
| 328 |
struct KeepPragma { |
| 329 |
// The line number where we saw the begin_keep or keep pragma. |
| 330 |
int SeenAtLine = 0; // 1-based line number. |
| 331 |
// true if it is a block begin/end_keep pragma; false if it is a |
| 332 |
// single-line keep pragma. |
| 333 |
bool Block = false; |
| 334 |
}; |
| 335 |
// A stack for tracking all open begin_keep pragmas or single-line keeps. |
| 336 |
std::vector<KeepPragma> KeepStack; |
| 337 |
}; |
| 338 |
|
| 339 |
void PragmaIncludes::record(const CompilerInstance &CI) { |
| 340 |
auto Record = std::make_unique<RecordPragma>(CI, this); |
| 341 |
CI.getPreprocessor().addCommentHandler(Record.get()); |
| 342 |
CI.getPreprocessor().addPPCallbacks(std::move(Record)); |
| 343 |
} |
| 344 |
|
| 345 |
llvm::StringRef PragmaIncludes::getPublic(const FileEntry *F) const { |
| 346 |
auto It = IWYUPublic.find(F->getUniqueID()); |
| 347 |
if (It == IWYUPublic.end()) |
| 348 |
return ""; |
| 349 |
return It->getSecond(); |
| 350 |
} |
| 351 |
|
| 352 |
static llvm::SmallVector<const FileEntry *> |
| 353 |
toFileEntries(llvm::ArrayRef<StringRef> FileNames, FileManager& FM) { |
| 354 |
llvm::SmallVector<const FileEntry *> Results; |
| 355 |
|
| 356 |
for (auto FName : FileNames) { |
| 357 |
// FIMXE: log the failing cases? |
| 358 |
if (auto FE = expectedToOptional(FM.getFileRef(FName))) |
| 359 |
Results.push_back(*FE); |
| 360 |
} |
| 361 |
return Results; |
| 362 |
} |
| 363 |
llvm::SmallVector<const FileEntry *> |
| 364 |
PragmaIncludes::getExporters(const FileEntry *File, FileManager &FM) const { |
| 365 |
auto It = IWYUExportBy.find(File->getUniqueID()); |
| 366 |
if (It == IWYUExportBy.end()) |
| 367 |
return {}; |
| 368 |
|
| 369 |
return toFileEntries(It->getSecond(), FM); |
| 370 |
} |
| 371 |
llvm::SmallVector<const FileEntry *> |
| 372 |
PragmaIncludes::getExporters(tooling::stdlib::Header StdHeader, |
| 373 |
FileManager &FM) const { |
| 374 |
auto It = StdIWYUExportBy.find(StdHeader); |
| 375 |
if (It == StdIWYUExportBy.end()) |
| 376 |
return {}; |
| 377 |
return toFileEntries(It->getSecond(), FM); |
| 378 |
} |
| 379 |
|
| 380 |
bool PragmaIncludes::isSelfContained(const FileEntry *FE) const { |
| 381 |
return !NonSelfContainedFiles.contains(FE->getUniqueID()); |
| 382 |
} |
| 383 |
|
| 384 |
bool PragmaIncludes::isPrivate(const FileEntry *FE) const { |
| 385 |
return IWYUPublic.find(FE->getUniqueID()) != IWYUPublic.end(); |
| 386 |
} |
| 387 |
|
| 388 |
namespace { |
| 389 |
template <typename T> bool isImplicitTemplateSpecialization(const Decl *D) { |
| 390 |
if (const auto *TD = dyn_cast<T>(D)) |
| 391 |
return TD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation; |
| 392 |
return false; |
| 393 |
} |
| 394 |
} // namespace |
| 395 |
|
| 396 |
std::unique_ptr<ASTConsumer> RecordedAST::record() { |
| 397 |
class Recorder : public ASTConsumer { |
| 398 |
RecordedAST *Out; |
| 399 |
|
| 400 |
public: |
| 401 |
Recorder(RecordedAST *Out) : Out(Out) {} |
| 402 |
void Initialize(ASTContext &Ctx) override { Out->Ctx = &Ctx; } |
| 403 |
bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 404 |
const auto &SM = Out->Ctx->getSourceManager(); |
| 405 |
for (Decl *D : DG) { |
| 406 |
if (!SM.isWrittenInMainFile(SM.getExpansionLoc(D->getLocation()))) |
| 407 |
continue; |
| 408 |
if (isImplicitTemplateSpecialization<FunctionDecl>(D) || |
| 409 |
isImplicitTemplateSpecialization<CXXRecordDecl>(D) || |
| 410 |
isImplicitTemplateSpecialization<VarDecl>(D)) |
| 411 |
continue; |
| 412 |
// FIXME: Filter out certain Obj-C as well. |
| 413 |
Out->Roots.push_back(D); |
| 414 |
} |
| 415 |
return ASTConsumer::HandleTopLevelDecl(DG); |
| 416 |
} |
| 417 |
}; |
| 418 |
|
| 419 |
return std::make_unique<Recorder>(this); |
| 420 |
} |
| 421 |
|
| 422 |
std::unique_ptr<PPCallbacks> RecordedPP::record(const Preprocessor &PP) { |
| 423 |
return std::make_unique<PPRecorder>(*this, PP); |
| 424 |
} |
| 425 |
|
| 426 |
} // namespace clang::include_cleaner |