Skip to content

Commit 280b04f

Browse files
authored
Record mainfile name in the Frontend time trace (#99866)
1 parent 28e6095 commit 280b04f

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

clang/lib/Parse/ParseAST.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,15 @@ void clang::ParseAST(Sema &S, bool PrintStats, bool SkipFunctionBodies) {
152152
bool HaveLexer = S.getPreprocessor().getCurrentLexer();
153153

154154
if (HaveLexer) {
155-
llvm::TimeTraceScope TimeScope("Frontend");
155+
llvm::TimeTraceScope TimeScope("Frontend", [&]() {
156+
llvm::TimeTraceMetadata M;
157+
if (llvm::isTimeTraceVerbose()) {
158+
const SourceManager &SM = S.getSourceManager();
159+
if (const auto *FE = SM.getFileEntryForID(SM.getMainFileID()))
160+
M.File = FE->tryGetRealPathName();
161+
}
162+
return M;
163+
});
156164
P.Initialize();
157165
Parser::DeclGroupPtrTy ADecl;
158166
Sema::ModuleImportState ImportState;

clang/unittests/Support/TimeProfilerTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ bool compileFromString(StringRef Code, StringRef Standard, StringRef File,
7676
}
7777

7878
std::string GetMetadata(json::Object *Event) {
79-
std::string Metadata;
80-
llvm::raw_string_ostream OS(Metadata);
79+
std::string M;
80+
llvm::raw_string_ostream OS(M);
8181
if (json::Object *Args = Event->getObject("args")) {
8282
if (auto Detail = Args->getString("detail"))
8383
OS << Detail;
8484
// Use only filename to not include os-specific path separators.
8585
if (auto File = Args->getString("file"))
86-
OS << ", " << llvm::sys::path::filename(*File);
86+
OS << (M.empty() ? "" : ", ") << llvm::sys::path::filename(*File);
8787
if (auto Line = Args->getInteger("line"))
8888
OS << ":" << *Line;
8989
}
90-
return Metadata;
90+
return M;
9191
}
9292

9393
// Returns pretty-printed trace graph.
@@ -209,7 +209,7 @@ constexpr int slow_init_list[] = {1, 1, 2, 3, 5, 8, 13, 21}; // 25th line
209209
ASSERT_TRUE(compileFromString(Code, "-std=c++20", "test.cc"));
210210
std::string Json = teardownProfiler();
211211
ASSERT_EQ(R"(
212-
Frontend
212+
Frontend (test.cc)
213213
| ParseDeclarationOrFunctionDefinition (test.cc:2:1)
214214
| ParseDeclarationOrFunctionDefinition (test.cc:6:1)
215215
| | ParseFunctionDefinition (slow_func)
@@ -266,7 +266,7 @@ TEST(TimeProfilerTest, TemplateInstantiations) {
266266
/*Headers=*/{{"a.h", A_H}, {"b.h", B_H}}));
267267
std::string Json = teardownProfiler();
268268
ASSERT_EQ(R"(
269-
Frontend
269+
Frontend (test.cc)
270270
| ParseFunctionDefinition (fooB)
271271
| ParseFunctionDefinition (fooMTA)
272272
| ParseFunctionDefinition (fooA)
@@ -291,7 +291,7 @@ struct {
291291
ASSERT_TRUE(compileFromString(Code, "-std=c99", "test.c"));
292292
std::string Json = teardownProfiler();
293293
ASSERT_EQ(R"(
294-
Frontend
294+
Frontend (test.c)
295295
| ParseDeclarationOrFunctionDefinition (test.c:2:1)
296296
| | isIntegerConstantExpr (<test.c:3:18>)
297297
| | EvaluateKnownConstIntCheckOverflow (<test.c:3:18>)

0 commit comments

Comments
 (0)