FindAllMacros.cpp
2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//===-- FindAllMacros.cpp - find all macros ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "FindAllMacros.h"
#include "HeaderMapCollector.h"
#include "PathConfig.h"
#include "SymbolInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/Token.h"
#include "llvm/Support/Path.h"
namespace clang {
namespace find_all_symbols {
llvm::Optional<SymbolInfo>
FindAllMacros::CreateMacroSymbol(const Token &MacroNameTok,
const MacroInfo *info) {
std::string FilePath =
getIncludePath(*SM, info->getDefinitionLoc(), Collector);
if (FilePath.empty())
return llvm::None;
return SymbolInfo(MacroNameTok.getIdentifierInfo()->getName(),
SymbolInfo::SymbolKind::Macro, FilePath, {});
}
void FindAllMacros::MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) {
if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo()))
++FileSymbols[*Symbol].Seen;
}
void FindAllMacros::MacroUsed(const Token &Name, const MacroDefinition &MD) {
if (!MD || !SM->isInMainFile(SM->getExpansionLoc(Name.getLocation())))
return;
if (auto Symbol = CreateMacroSymbol(Name, MD.getMacroInfo()))
++FileSymbols[*Symbol].Used;
}
void FindAllMacros::MacroExpands(const Token &MacroNameTok,
const MacroDefinition &MD, SourceRange Range,
const MacroArgs *Args) {
MacroUsed(MacroNameTok, MD);
}
void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) {
MacroUsed(MacroNameTok, MD);
}
void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroDefinition &MD) {
MacroUsed(MacroNameTok, MD);
}
void FindAllMacros::EndOfMainFile() {
Reporter->reportSymbols(SM->getFileEntryForID(SM->getMainFileID())->getName(),
FileSymbols);
FileSymbols.clear();
}
} // namespace find_all_symbols
} // namespace clang