-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.cpp
More file actions
281 lines (234 loc) · 9.37 KB
/
scanner.cpp
File metadata and controls
281 lines (234 loc) · 9.37 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// scanner.cpp
// 목적:
// - 지정한 소스들(.cpp)을 AST로 파싱하여 "모든 함수 호출"을 전수 조사
// - 각 호출의 예외 명세(noexcept 여부)를 판정하여 CSV로 출력
//
// 출력 컬럼:
// file,line,col,kind,qualified-name,noexcept,signature,callee-source
//
// 빌드/실행은 README의 "사용 개요" 참조
//
// 주의사항:
// - 시스템 헤더 안의 호출은 기본 제외(필요 시 주석 해제하여 포함 가능)
// - noexcept 판정은 FunctionProtoType::isNothrow() 결과를 사용
// - 생성자, 멤버함수, 자유함수, 연산자 호출을 모두 처리
// - 구현(libstdc++/libc++/MSVC STL)과 템플릿 인스턴스에 따라 결과는 달라질 수 있음
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include "clang/AST/ASTContext.h"
#include "clang/AST/Type.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/CommandLine.h"
using namespace clang;
using namespace clang::tooling;
using namespace clang::ast_matchers;
namespace {
// ---------------------- 옵션 정의 ----------------------
// std:: 네임스페이스 하위 호출만 보고 싶다면 1
static llvm::cl::opt<bool> OnlyStd(
"only-std",
llvm::cl::desc("std:: 네임스페이스 호출만 보고 (default: 0)"),
llvm::cl::init(0));
// 접두 필터: "std::filesystem::" 처럼 특정 도메인만 걸러내기
static llvm::cl::opt<std::string> NamePrefix(
"name-prefix",
llvm::cl::desc("정규화 이름 접두 필터(예: std::filesystem::)"),
llvm::cl::init(""));
// CSV 헤더 출력 여부
static llvm::cl::opt<bool> CsvHeader(
"csv-header",
llvm::cl::desc("CSV 헤더 출력 (default: 0)"),
llvm::cl::init(0));
// ---------------------- 유틸 함수 ----------------------
// CSV 안전한 인용: 내부의 " 를 "" 로 이스케이프하고 전체를 "..." 로 감싼다.
static std::string csv_quote(std::string s) {
std::string out;
out.reserve(s.size() + 2);
out.push_back('"');
for (char c : s) {
if (c == '"') out += "\"\"";
else out.push_back(c);
}
out.push_back('"');
return out;
}
// 소스 텍스트 추출(호출 지점 표시용, 너무 길면 자를 수 있음)
static std::string get_source_snippet(const SourceRange& R,
const SourceManager& SM,
const LangOptions& LO) {
CharSourceRange CR = CharSourceRange::getTokenRange(R);
llvm::StringRef SR = Lexer::getSourceText(CR, SM, LO);
std::string s = SR.str();
// 너무 긴 경우 절단(가독 목적)
const size_t MAX_LEN = 200;
if (s.size() > MAX_LEN) {
s.erase(MAX_LEN);
s += "...";
}
return s;
}
// 함수(또는 생성자)의 정규화 이름 작성
static std::string get_qualified_name(const Decl* D) {
if (!D) return {};
if (const auto* FD = llvm::dyn_cast<FunctionDecl>(D)) {
return FD->getQualifiedNameAsString();
}
if (const auto* CD = llvm::dyn_cast<CXXConstructorDecl>(D)) {
// 생성자는 클래스 이름을 반환
return CD->getParent()->getQualifiedNameAsString() + "::" +
CD->getNameAsString();
}
if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(D)) {
return MD->getQualifiedNameAsString();
}
return {};
}
// 함수 시그니처(Pretty) 생성
static std::string get_pretty_signature(const FunctionDecl* FD) {
if (!FD) return {};
std::string Sig;
llvm::raw_string_ostream OS(Sig);
FD->print(OS);
return OS.str();
}
// 예외 명세(noexcept) 판정
static bool is_nothrow_function(const FunctionDecl* FD) {
if (!FD) return false;
const Type* Ty = FD->getType().getTypePtrOrNull();
if (!Ty) return false;
if (const auto* FPT = Ty->getAs<FunctionProtoType>()) {
return FPT->isNothrow();
}
return false;
}
// ---------------------- 콜백 ----------------------
struct CallDump : public MatchFinder::MatchCallback {
const SourceManager* SM{nullptr};
const LangOptions* LO{nullptr};
void run(const MatchFinder::MatchResult& Result) override {
if (!SM) SM = Result.SourceManager;
if (!LO) LO = &Result.Context->getLangOpts();
// 다양한 호출 형태를 한 번에 처리
if (const auto* CE = Result.Nodes.getNodeAs<CallExpr>("call")) {
handleCallExpr(*CE, Result);
}
if (const auto* ME = Result.Nodes.getNodeAs<CXXMemberCallExpr>("mcall")) {
handleMemberCallExpr(*ME, Result);
}
if (const auto* CX = Result.Nodes.getNodeAs<CXXConstructExpr>("ctor")) {
handleConstructExpr(*CX, Result);
}
if (const auto* OE = Result.Nodes.getNodeAs<CXXOperatorCallExpr>("opcall")) {
handleOperatorCallExpr(*OE, Result);
}
}
void handleCallExpr(const CallExpr& CE, const MatchFinder::MatchResult& R) {
const FunctionDecl* FD = CE.getDirectCallee();
if (!FD) return;
std::string qname = get_qualified_name(FD);
if (OnlyStd && qname.rfind("std::", 0) != 0) return;
if (!NamePrefix.empty() && qname.rfind(NamePrefix, 0) != 0) return;
bool nothrow = is_nothrow_function(FD);
PresumedLoc PLoc = SM->getPresumedLoc(CE.getExprLoc());
std::string sig = get_pretty_signature(FD);
std::string snippet = get_source_snippet(CE.getSourceRange(), *SM, *LO);
print_csv(PLoc, "call", qname, nothrow, sig, snippet);
}
void handleMemberCallExpr(const CXXMemberCallExpr& ME,
const MatchFinder::MatchResult& R) {
const FunctionDecl* FD = ME.getDirectCallee();
if (!FD) return;
std::string qname = get_qualified_name(FD);
if (OnlyStd && qname.rfind("std::", 0) != 0) return;
if (!NamePrefix.empty() && qname.rfind(NamePrefix, 0) != 0) return;
bool nothrow = is_nothrow_function(FD);
PresumedLoc PLoc = SM->getPresumedLoc(ME.getExprLoc());
std::string sig = get_pretty_signature(FD);
std::string snippet = get_source_snippet(ME.getSourceRange(), *SM, *LO);
print_csv(PLoc, "member-call", qname, nothrow, sig, snippet);
}
void handleConstructExpr(const CXXConstructExpr& CX,
const MatchFinder::MatchResult& R) {
const FunctionDecl* FD = CX.getConstructor();
if (!FD) return;
std::string qname = get_qualified_name(FD);
if (OnlyStd && qname.rfind("std::", 0) != 0) return;
if (!NamePrefix.empty() && qname.rfind(NamePrefix, 0) != 0) return;
bool nothrow = is_nothrow_function(FD);
PresumedLoc PLoc = SM->getPresumedLoc(CX.getExprLoc());
std::string sig = get_pretty_signature(FD);
std::string snippet = get_source_snippet(CX.getSourceRange(), *SM, *LO);
print_csv(PLoc, "construct", qname, nothrow, sig, snippet);
}
void handleOperatorCallExpr(const CXXOperatorCallExpr& OE,
const MatchFinder::MatchResult& R) {
const FunctionDecl* FD = OE.getDirectCallee();
if (!FD) return;
std::string qname = get_qualified_name(FD);
if (OnlyStd && qname.rfind("std::", 0) != 0) return;
if (!NamePrefix.empty() && qname.rfind(NamePrefix, 0) != 0) return;
bool nothrow = is_nothrow_function(FD);
PresumedLoc PLoc = SM->getPresumedLoc(OE.getExprLoc());
std::string sig = get_pretty_signature(FD);
std::string snippet = get_source_snippet(OE.getSourceRange(), *SM, *LO);
print_csv(PLoc, "operator-call", qname, nothrow, sig, snippet);
}
void print_csv(const PresumedLoc& P,
const char* kind,
const std::string& qname,
bool nothrow,
const std::string& sig,
const std::string& snippet) {
std::string file = P.isValid() ? P.getFilename() : "<unknown>";
unsigned line = P.isValid() ? P.getLine() : 0;
unsigned col = P.isValid() ? P.getColumn() : 0;
// CSV: file,line,col,kind,qualified-name,noexcept,signature,callee-source
std::cout
<< csv_quote(file) << ","
<< line << ","
<< col << ","
<< csv_quote(kind) << ","
<< csv_quote(qname) << ","
<< (nothrow ? "noexcept" : "may-throw") << ","
<< csv_quote(sig) << ","
<< csv_quote(snippet)
<< "\n";
}
};
} // namespace
int main(int argc, const char** argv) {
llvm::cl::OptionCategory Cat("std-call-scan options");
auto ExpectedParser = CommonOptionsParser::create(argc, argv, Cat);
if (!ExpectedParser) {
llvm::errs() << ExpectedParser.takeError();
return 1;
}
CommonOptionsParser& OptionsParser = ExpectedParser.get();
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
if (CsvHeader) {
std::cout
<< "file,line,col,kind,qualified-name,noexcept,signature,callee-source\n";
}
// 매처: 호출 4종 모두 (시스템 헤더 제외)
auto M1 = callExpr(unless(isExpansionInSystemHeader())).bind("call");
auto M2 = cxxMemberCallExpr(unless(isExpansionInSystemHeader())).bind("mcall");
auto M3 = cxxConstructExpr(unless(isExpansionInSystemHeader())).bind("ctor");
auto M4 = cxxOperatorCallExpr(unless(isExpansionInSystemHeader())).bind("opcall");
// 시스템 헤더까지 포함하려면 위의 unless(...)를 제거하세요.
CallDump Printer;
MatchFinder Finder;
Finder.addMatcher(M1, &Printer);
Finder.addMatcher(M2, &Printer);
Finder.addMatcher(M3, &Printer);
Finder.addMatcher(M4, &Printer);
return Tool.run(newFrontendActionFactory(&Finder).get());
}