clang-tools 10.0.0

FindAllMacros.cpp
Go to the documentation of this file.
1 //===-- FindAllMacros.cpp - find all macros ---------------------*- C++ -*-===//
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 "FindAllMacros.h"
10 #include "HeaderMapCollector.h"
11 #include "PathConfig.h"
12 #include "SymbolInfo.h"
13 #include "clang/Basic/IdentifierTable.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Lex/MacroInfo.h"
16 #include "clang/Lex/Token.h"
17 #include "llvm/Support/Path.h"
18
19 namespace clang {
20 namespace find_all_symbols {
21
22 llvm::Optional<SymbolInfo>
23 FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
24 const MacroInfo *info) {
25 std::string FilePath =
26 getIncludePath(*SM, info->getDefinitionLoc(), Collector);
27 if (FilePath.empty())
28 return llvm::None;
29 return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
30 SymbolInfo::SymbolKind::Macro, FilePath, {});
31 }
32
33 void FindAllMacros::MacroDefined(const Token &MacroNameTok,
34 const MacroDirective *MD) {
35 if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo()))
36 ++FileSymbols[*Symbol].Seen;
37 }
38
39 void FindAllMacros::MacroUsed(const Token &Name, const MacroDefinition &MD) {
40 if (!MD || !SM->isInMainFile(SM->getExpansionLoc(Name.getLocation())))
41 return;
42 if (auto Symbol = CreateMacroSymbol(Name, MD.getMacroInfo()))
43 ++FileSymbols[*Symbol].Used;
44 }
45
46 void FindAllMacros::MacroExpands(const Token &MacroNameTok,
47 const MacroDefinition &MD, SourceRange Range,
48 const MacroArgs *Args) {
49 MacroUsed(MacroNameTok, MD);
50 }
51
52 void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
53 const MacroDefinition &MD) {
54 MacroUsed(MacroNameTok, MD);
55 }
56
57 void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
58 const MacroDefinition &MD) {
59 MacroUsed(MacroNameTok, MD);
60 }
61
62 void FindAllMacros::EndOfMainFile() {
63 Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
64 FileSymbols);
65 FileSymbols.clear();
66 }
67
68 } // namespace find_all_symbols
69 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::find_all_symbols::FindAllMacros::Ifdef
void Ifdef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: FindAllMacros.cpp:52
clang::find_all_symbols::SymbolReporter::reportSymbols
virtual void reportSymbols(llvm::StringRef FileName, const SymbolInfo::SignalMap &Symbols)=0
HeaderMapCollector.h
clang::find_all_symbols::FindAllMacros::MacroExpands
void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override
Definition: FindAllMacros.cpp:46
clang::find_all_symbols::getIncludePath
std::string getIncludePath(const SourceManager &SM, SourceLocation Loc, const HeaderMapCollector *Collector)
This calculates the include path for Loc.
Definition: PathConfig.cpp:16
clang::find_all_symbols::FindAllMacros::EndOfMainFile
void EndOfMainFile() override
Definition: FindAllMacros.cpp:62
SymbolInfo.h
clang::find_all_symbols::FindAllMacros::Ifndef
void Ifndef(SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) override
Definition: FindAllMacros.cpp:57
SymbolInfo
clang::find_all_symbols::SymbolInfo SymbolInfo
Definition: FindAllSymbolsMain.cpp:38
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::find_all_symbols::FindAllMacros::MacroDefined
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) override
Definition: FindAllMacros.cpp:33
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::doc::MD
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
Range
CharSourceRange Range
SourceRange for the file name.
Definition: IncludeOrderCheck.cpp:38
clang::find_all_symbols::SymbolInfo::SymbolKind::Macro
FindAllMacros.h
PathConfig.h