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