clang 20.0.0git
Classes | Public Member Functions | List of all members
clang::format::MacroCallReconstructor Class Reference

Converts a sequence of UnwrappedLines containing expanded macros into a single UnwrappedLine containing the macro calls. More...

#include "/home/buildbot/as-worker-4/publish-doxygen-docs/llvm-project/clang/lib/Format/Macros.h"

Public Member Functions

 MacroCallReconstructor (unsigned Level, const llvm::DenseMap< FormatToken *, std::unique_ptr< UnwrappedLine > > &ActiveExpansions)
 Create an Reconstructor whose resulting UnwrappedLine will start at Level, using the map from name identifier token to the corresponding tokens of the spelled macro call.
 
void addLine (const UnwrappedLine &Line)
 For the given Line, match all occurences of tokens expanded from a macro to unwrapped lines in the spelled macro call so that the resulting tree of unwrapped lines best resembles the structure of unwrapped lines passed in via addLine.
 
bool finished () const
 Check whether at the current state there is no open macro expansion that needs to be processed to finish an macro call.
 
UnwrappedLine takeResult () &&
 Retrieve the formatted UnwrappedLine containing the orginal macro calls, formatted according to the expanded token stream received via addLine().
 

Detailed Description

Converts a sequence of UnwrappedLines containing expanded macros into a single UnwrappedLine containing the macro calls.

This UnwrappedLine may be broken into child lines, in a way that best conveys the structure of the expanded code.

In the simplest case, a spelled UnwrappedLine contains one macro, and after expanding it we have one expanded UnwrappedLine. In general, macro expansions can span UnwrappedLines, and multiple macros can contribute tokens to the same line. We keep consuming expanded lines until:

A single UnwrappedLine represents this chunk of code.

After this point, the state of the spelled/expanded stream is "in sync" (both at the start of an UnwrappedLine, with no macros open), so the Reconstructor can be thrown away and parsing can continue.

Given a mapping from the macro name identifier token in the macro call to the tokens of the macro call, for example: CLASSA -> CLASSA({public: void x();})

When getting the formatted lines of the expansion via the addLine method (each '->' specifies a call to addLine ): -> class A { -> public: -> void x(); -> };

Creates the tree of unwrapped lines containing the macro call tokens so that the macro call tokens fit the semantic structure of the expanded formatted lines: -> CLASSA({ -> public: -> void x(); -> })

Definition at line 173 of file Macros.h.

Constructor & Destructor Documentation

◆ MacroCallReconstructor()

clang::format::MacroCallReconstructor::MacroCallReconstructor ( unsigned  Level,
const llvm::DenseMap< FormatToken *, std::unique_ptr< UnwrappedLine > > &  ActiveExpansions 
)

Create an Reconstructor whose resulting UnwrappedLine will start at Level, using the map from name identifier token to the corresponding tokens of the spelled macro call.

Definition at line 43 of file MacroCallReconstructor.cpp.

Member Function Documentation

◆ addLine()

void clang::format::MacroCallReconstructor::addLine ( const UnwrappedLine Line)

For the given Line, match all occurences of tokens expanded from a macro to unwrapped lines in the spelled macro call so that the resulting tree of unwrapped lines best resembles the structure of unwrapped lines passed in via addLine.

Definition at line 52 of file MacroCallReconstructor.cpp.

References finished(), clang::First, clang::format::forEachToken(), clang::Line, and Parent.

◆ finished()

bool clang::format::MacroCallReconstructor::finished ( ) const
inline

Check whether at the current state there is no open macro expansion that needs to be processed to finish an macro call.

Only when finished() is true, takeResult() can be called to retrieve the resulting UnwrappedLine. If there are multiple subsequent macro calls within an unwrapped line in the spelled token stream, the calling code may also continue to call addLine() when finished() is true.

Definition at line 196 of file Macros.h.

Referenced by addLine().

◆ takeResult()

UnwrappedLine clang::format::MacroCallReconstructor::takeResult ( ) &&

Retrieve the formatted UnwrappedLine containing the orginal macro calls, formatted according to the expanded token stream received via addLine().

Generally, this line tries to have the same structure as the expanded, formatted unwrapped lines handed in via addLine(), with the exception that for multiple top-level lines, each subsequent line will be the child of the last token in its predecessor. This representation is chosen because it is a precondition to the formatter that we get what looks like a single statement in a single UnwrappedLine (i.e. matching parens).

If a token in a macro argument is a child of a token in the expansion, the parent will be the corresponding token in the macro call. For example: #define C(a, b) class C { a b C(int x;, int y;) would expand to class C { int x; int y; where in a formatted line "int x;" and "int y;" would both be new separate lines.

In the result, "int x;" will be a child of the opening parenthesis in "C(" and "int y;" will be a child of the "," token: C ( - int x; , - int y; )

Definition at line 60 of file MacroCallReconstructor.cpp.

References clang::finalize(), and clang::Result.


The documentation for this class was generated from the following files: