[clang] [clang][analyzer] Add plist macro formatting (PR #156046)
DonĂ¡t Nagy via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 24 07:31:48 PST 2025
================
@@ -132,6 +133,41 @@ MacroExpansionContext::getOriginalText(SourceLocation MacroExpansionLoc) const {
LangOpts);
}
+std::optional<StringRef> MacroExpansionContext::getFormattedExpandedText(
+ SourceLocation MacroExpansionLoc, bool ShouldFormatMacrosPlist) const {
+ std::optional<StringRef> ExpandedText = getExpandedText(MacroExpansionLoc);
+ if (!ExpandedText)
+ return std::nullopt;
+
+ if (!ShouldFormatMacrosPlist)
+ return ExpandedText;
+
+ auto CachedIt = FormattedExpandedTokens.find(MacroExpansionLoc);
+ if (CachedIt != FormattedExpandedTokens.end())
+ return StringRef(CachedIt->getSecond());
+
+ clang::format::FormatStyle Style = clang::format::getLLVMStyle();
+
+ std::string MacroCodeBlock = ExpandedText->str();
+
+ std::vector<clang::tooling::Range> Ranges;
+ Ranges.emplace_back(0, MacroCodeBlock.length());
+
+ auto Replacements = clang::format::reformat(Style, MacroCodeBlock, Ranges,
+ "<macro-expansion>");
+
+ auto Result =
----------------
NagyDonat wrote:
Using `auto` makes these two declarations a bit cryptic -- unless the types are complex and ugly, I would prefer spelling them out explicitly to make the code easier to read. (I know that many IDEs show the type in tooltips, but it is not available e.g. in this review.)
(Note that e.g. `auto CachedIt` is fine, because (1) it is well-known that `find` returns an iterator (2) iterator types are complex and ugly.)
https://github.com/llvm/llvm-project/pull/156046
More information about the cfe-commits
mailing list