Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions clang/include/clang/Analysis/MacroExpansionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class MacroExpansionContext {
std::optional<StringRef>
getOriginalText(SourceLocation MacroExpansionLoc) const;

/// \param MacroExpansionLoc Must be the expansion location of a macro.
/// \return A formatted representation of the textual representation of the
/// token sequence which was substituted in place of the macro.
/// If no macro was expanded at that location, returns std::nullopt.
std::optional<StringRef>
getFormattedExpandedText(SourceLocation MacroExpansionLoc) const;

LLVM_DUMP_METHOD void dumpExpansionRangesToStream(raw_ostream &OS) const;
LLVM_DUMP_METHOD void dumpExpandedTextsToStream(raw_ostream &OS) const;
LLVM_DUMP_METHOD void dumpExpansionRanges() const;
Expand All @@ -106,6 +113,7 @@ class MacroExpansionContext {
using MacroExpansionText = SmallString<40>;
using ExpansionMap = llvm::DenseMap<SourceLocation, MacroExpansionText>;
using ExpansionRangeMap = llvm::DenseMap<SourceLocation, SourceLocation>;
using FormattedExpansionMap = llvm::DenseMap<SourceLocation, std::string>;

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

/// Caches formatted macro expansions keyed by expansion location.
mutable FormattedExpansionMap FormattedExpandedTokens;

Preprocessor *PP = nullptr;
SourceManager *SM = nullptr;
const LangOptions &LangOpts;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_clang_library(clangAnalysis
clangAST
clangASTMatchers
clangBasic
clangFormat
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I am supportive of using formatted text in reports. But I think we need an RFC in discourse before we link format with the main Clang library.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clangLex

DEPENDS
Expand Down
30 changes: 30 additions & 0 deletions clang/lib/Analysis/MacroExpansionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "clang/Analysis/MacroExpansionContext.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Debug.h"
#include <optional>

Expand Down Expand Up @@ -132,6 +133,35 @@ MacroExpansionContext::getOriginalText(SourceLocation MacroExpansionLoc) const {
LangOpts);
}

std::optional<StringRef> MacroExpansionContext::getFormattedExpandedText(
SourceLocation MacroExpansionLoc) const {
std::optional<StringRef> ExpandedText = getExpandedText(MacroExpansionLoc);
if (!ExpandedText)
return std::nullopt;

auto [It, Inserted] =
FormattedExpandedTokens.try_emplace(MacroExpansionLoc, "");
if (!Inserted)
return StringRef(It->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());

clang::tooling::Replacements Replacements = clang::format::reformat(
Style, MacroCodeBlock, Ranges, "<macro-expansion>");

llvm::Expected<std::string> Result =
clang::tooling::applyAllReplacements(MacroCodeBlock, Replacements);

It->getSecond() = Result ? std::move(*Result) : std::move(MacroCodeBlock);

return StringRef(It->getSecond());
}

void MacroExpansionContext::dumpExpansionRanges() const {
dumpExpansionRangesToStream(llvm::dbgs());
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Core/PlistDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ getExpandedMacro(SourceLocation MacroExpansionLoc,
const SourceManager &SM) {
if (auto CTUMacroExpCtx =
CTU.getMacroExpansionContextForSourceLocation(MacroExpansionLoc)) {
return CTUMacroExpCtx->getExpandedText(MacroExpansionLoc);
return CTUMacroExpCtx->getFormattedExpandedText(MacroExpansionLoc);
}
return MacroExpansions.getExpandedText(MacroExpansionLoc);
return MacroExpansions.getFormattedExpandedText(MacroExpansionLoc);
}
4 changes: 2 additions & 2 deletions clang/test/Analysis/plist-macros-with-expansion-ctu.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void test3(void) {
// CHECK-NEXT: <key>file</key><integer>0</integer>
// CHECK-NEXT: </dict>
// CHECK-NEXT: <key>name</key><string>M</string>
// CHECK-NEXT: <key>expansion</key><string>F1 (&amp;X )</string>
// CHECK-NEXT: <key>expansion</key><string>F1
// CHECK-NEXT: </dict>
// CHECK-NEXT: </array>

Expand All @@ -89,7 +89,7 @@ void test4(void) {
// CHECK-NEXT: <key>file</key><integer>0</integer>
// CHECK-NEXT: </dict>
// CHECK-NEXT: <key>name</key><string>M</string>
// CHECK-NEXT: <key>expansion</key><string>F2 (&amp;X )</string>
// CHECK-NEXT: <key>expansion</key><string>F2
// CHECK-NEXT: </dict>
// CHECK-NEXT: </array>

Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/plist-macros-with-expansion.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void test_strange_macro_expansion(void) {
// CHECK-NEXT: <key>file</key><integer>0</integer>
// CHECK-NEXT: </dict>
// CHECK-NEXT: <key>name</key><string>STRANGE_FN(path)</string>
// CHECK-NEXT: <key>expansion</key><string>STRANGE_FN (path ,0)</string>
// CHECK-NEXT: <key>expansion</key><string>STRANGE_FN
// CHECK-NEXT: </dict>
// CHECK-NEXT: </array>

Loading