Skip to content

Commit c8f7f39

Browse files
committed
Add clang-format macro
1 parent 6459f39 commit c8f7f39

File tree

8 files changed

+154
-139
lines changed

8 files changed

+154
-139
lines changed

clang/include/clang/Analysis/MacroExpansionContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ class MacroExpansionContext {
9696
std::optional<StringRef>
9797
getOriginalText(SourceLocation MacroExpansionLoc) const;
9898

99+
/// \param MacroExpansionLoc Must be the expansion location of a macro.
100+
/// \return A formatted representation of the textual representation of the
101+
/// token sequence which was substituted in place of the macro.
102+
/// If no macro was expanded at that location, returns std::nullopt.
103+
std::optional<StringRef>
104+
getFormattedExpandedText(SourceLocation MacroExpansionLoc) const;
105+
99106
LLVM_DUMP_METHOD void dumpExpansionRangesToStream(raw_ostream &OS) const;
100107
LLVM_DUMP_METHOD void dumpExpandedTextsToStream(raw_ostream &OS) const;
101108
LLVM_DUMP_METHOD void dumpExpansionRanges() const;
@@ -106,6 +113,7 @@ class MacroExpansionContext {
106113
using MacroExpansionText = SmallString<40>;
107114
using ExpansionMap = llvm::DenseMap<SourceLocation, MacroExpansionText>;
108115
using ExpansionRangeMap = llvm::DenseMap<SourceLocation, SourceLocation>;
116+
using FormattedExpansionMap = llvm::DenseMap<SourceLocation, std::string>;
109117

110118
/// Associates the textual representation of the expanded tokens at the given
111119
/// macro expansion location.
@@ -115,6 +123,9 @@ class MacroExpansionContext {
115123
/// substitution starting from a given macro expansion location.
116124
ExpansionRangeMap ExpansionRanges;
117125

126+
/// Caches formatted macro expansions keyed by expansion location.
127+
mutable FormattedExpansionMap FormattedExpandedTokens;
128+
118129
Preprocessor *PP = nullptr;
119130
SourceManager *SM = nullptr;
120131
const LangOptions &LangOpts;

clang/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_clang_library(clangAnalysis
4040
clangAST
4141
clangASTMatchers
4242
clangBasic
43+
clangFormat
4344
clangLex
4445

4546
DEPENDS

clang/lib/Analysis/MacroExpansionContext.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "clang/Analysis/MacroExpansionContext.h"
10+
#include "clang/Format/Format.h"
1011
#include "llvm/Support/Debug.h"
1112
#include <optional>
1213

@@ -132,6 +133,35 @@ MacroExpansionContext::getOriginalText(SourceLocation MacroExpansionLoc) const {
132133
LangOpts);
133134
}
134135

136+
std::optional<StringRef> MacroExpansionContext::getFormattedExpandedText(
137+
SourceLocation MacroExpansionLoc) const {
138+
std::optional<StringRef> ExpandedText = getExpandedText(MacroExpansionLoc);
139+
if (!ExpandedText)
140+
return std::nullopt;
141+
142+
auto [It, Inserted] =
143+
FormattedExpandedTokens.try_emplace(MacroExpansionLoc, "");
144+
if (!Inserted)
145+
return StringRef(It->getSecond());
146+
147+
clang::format::FormatStyle Style = clang::format::getLLVMStyle();
148+
149+
std::string MacroCodeBlock = ExpandedText->str();
150+
151+
std::vector<clang::tooling::Range> Ranges;
152+
Ranges.emplace_back(0, MacroCodeBlock.length());
153+
154+
clang::tooling::Replacements Replacements = clang::format::reformat(
155+
Style, MacroCodeBlock, Ranges, "<macro-expansion>");
156+
157+
llvm::Expected<std::string> Result =
158+
clang::tooling::applyAllReplacements(MacroCodeBlock, Replacements);
159+
160+
It->getSecond() = Result ? std::move(*Result) : std::move(MacroCodeBlock);
161+
162+
return StringRef(It->getSecond());
163+
}
164+
135165
void MacroExpansionContext::dumpExpansionRanges() const {
136166
dumpExpansionRangesToStream(llvm::dbgs());
137167
}

clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ getExpandedMacro(SourceLocation MacroExpansionLoc,
835835
const SourceManager &SM) {
836836
if (auto CTUMacroExpCtx =
837837
CTU.getMacroExpansionContextForSourceLocation(MacroExpansionLoc)) {
838-
return CTUMacroExpCtx->getExpandedText(MacroExpansionLoc);
838+
return CTUMacroExpCtx->getFormattedExpandedText(MacroExpansionLoc);
839839
}
840-
return MacroExpansions.getExpandedText(MacroExpansionLoc);
840+
return MacroExpansions.getFormattedExpandedText(MacroExpansionLoc);
841841
}

clang/test/Analysis/plist-macros-with-expansion-ctu.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void test3(void) {
6666
// CHECK-NEXT: <key>file</key><integer>0</integer>
6767
// CHECK-NEXT: </dict>
6868
// CHECK-NEXT: <key>name</key><string>M</string>
69-
// CHECK-NEXT: <key>expansion</key><string>F1 (&amp;X )</string>
69+
// CHECK-NEXT: <key>expansion</key><string>F1
7070
// CHECK-NEXT: </dict>
7171
// CHECK-NEXT: </array>
7272

@@ -89,7 +89,7 @@ void test4(void) {
8989
// CHECK-NEXT: <key>file</key><integer>0</integer>
9090
// CHECK-NEXT: </dict>
9191
// CHECK-NEXT: <key>name</key><string>M</string>
92-
// CHECK-NEXT: <key>expansion</key><string>F2 (&amp;X )</string>
92+
// CHECK-NEXT: <key>expansion</key><string>F2
9393
// CHECK-NEXT: </dict>
9494
// CHECK-NEXT: </array>
9595

clang/test/Analysis/plist-macros-with-expansion.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void test_strange_macro_expansion(void) {
2222
// CHECK-NEXT: <key>file</key><integer>0</integer>
2323
// CHECK-NEXT: </dict>
2424
// CHECK-NEXT: <key>name</key><string>STRANGE_FN(path)</string>
25-
// CHECK-NEXT: <key>expansion</key><string>STRANGE_FN (path ,0)</string>
25+
// CHECK-NEXT: <key>expansion</key><string>STRANGE_FN
2626
// CHECK-NEXT: </dict>
2727
// CHECK-NEXT: </array>
2828

0 commit comments

Comments
 (0)