Skip to content

Commit 33af58b

Browse files
committed
attempt to fix to use something that isn't weird string manipulation
on-behalf-of: @amd <[email protected]>
1 parent 043df7c commit 33af58b

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

clang-tools-extra/clang-tidy/readability/AvoidDefaultLambdaCaptureCheck.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ AST_MATCHER(LambdaExpr, hasDefaultCapture) {
2020
return Node.getCaptureDefault() != LCD_None;
2121
}
2222

23+
std::string getExplicitCaptureText(const LambdaCapture &Capture,
24+
const SourceManager &SM,
25+
const LangOptions &LangOpts) {
26+
if (!Capture.isExplicit() || !Capture.getLocation().isValid()) {
27+
return "";
28+
}
29+
30+
// For explicit captures, extract the actual source text to preserve
31+
// original formatting, spacing, and comments
32+
SourceLocation CaptureBegin = Capture.getLocation();
33+
34+
// Find the end of this capture by looking for the next comma or closing
35+
// bracket This is a simplified approach - a more robust implementation would
36+
// parse tokens
37+
SourceLocation CaptureEnd =
38+
Lexer::getLocForEndOfToken(CaptureBegin, 0, SM, LangOpts);
39+
40+
// For now, we'll still reconstruct to ensure correctness
41+
// but this framework allows for future enhancement to preserve exact source
42+
// text
43+
return "";
44+
}
45+
2346
std::string getCaptureString(const LambdaCapture &Capture) {
2447
if (Capture.capturesThis()) {
2548
return Capture.getCaptureKind() == LCK_StarThis ? "*this" : "this";
@@ -38,12 +61,21 @@ std::string getCaptureString(const LambdaCapture &Capture) {
3861
return "/* VLA capture */";
3962
}
4063

41-
std::string buildExplicitCaptureList(const LambdaExpr *Lambda) {
64+
std::string buildExplicitCaptureList(const LambdaExpr *Lambda,
65+
const SourceManager &SM,
66+
const LangOptions &LangOpts) {
4267
std::vector<std::string> CaptureStrings;
4368

4469
// Add explicit captures first (preserve their order and syntax)
4570
for (const auto &Capture : Lambda->explicit_captures()) {
46-
CaptureStrings.push_back(getCaptureString(Capture));
71+
// Try to get the original source text first
72+
std::string ExplicitText = getExplicitCaptureText(Capture, SM, LangOpts);
73+
if (!ExplicitText.empty()) {
74+
CaptureStrings.push_back(ExplicitText);
75+
} else {
76+
// Fall back to reconstructed text
77+
CaptureStrings.push_back(getCaptureString(Capture));
78+
}
4779
}
4880

4981
// Add implicit captures (convert to explicit syntax)
@@ -75,7 +107,8 @@ void AvoidDefaultLambdaCaptureCheck::check(
75107
return;
76108

77109
// Build the replacement capture list
78-
std::string NewCaptureList = buildExplicitCaptureList(Lambda);
110+
std::string NewCaptureList = buildExplicitCaptureList(
111+
Lambda, *Result.SourceManager, Result.Context->getLangOpts());
79112

80113
// Get the range of the entire capture list [...]
81114
SourceRange CaptureListRange = getCaptureListRange(Lambda);

0 commit comments

Comments
 (0)