xref: /freebsd-src/contrib/llvm-project/clang/lib/Format/Macros.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1753f127fSDimitry Andric //===--- Macros.h - Format C++ code -----------------------------*- C++ -*-===//
2e8d8bef9SDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric ///
9e8d8bef9SDimitry Andric /// \file
10e8d8bef9SDimitry Andric /// This file contains the main building blocks of macro support in
11e8d8bef9SDimitry Andric /// clang-format.
12e8d8bef9SDimitry Andric ///
13e8d8bef9SDimitry Andric /// In order to not violate the requirement that clang-format can format files
14e8d8bef9SDimitry Andric /// in isolation, clang-format's macro support uses expansions users provide
15e8d8bef9SDimitry Andric /// as part of clang-format's style configuration.
16e8d8bef9SDimitry Andric ///
17e8d8bef9SDimitry Andric /// Macro definitions are of the form "MACRO(p1, p2)=p1 + p2", but only support
18e8d8bef9SDimitry Andric /// one level of expansion (\see MacroExpander for a full description of what
19e8d8bef9SDimitry Andric /// is supported).
20e8d8bef9SDimitry Andric ///
21e8d8bef9SDimitry Andric /// As part of parsing, clang-format uses the MacroExpander to expand the
22e8d8bef9SDimitry Andric /// spelled token streams into expanded token streams when it encounters a
23e8d8bef9SDimitry Andric /// macro call. The UnwrappedLineParser continues to parse UnwrappedLines
24e8d8bef9SDimitry Andric /// from the expanded token stream.
25753f127fSDimitry Andric /// After the expanded unwrapped lines are parsed, the MacroCallReconstructor
26753f127fSDimitry Andric /// matches the spelled token stream into unwrapped lines that best resemble the
27753f127fSDimitry Andric /// structure of the expanded unwrapped lines. These reconstructed unwrapped
28753f127fSDimitry Andric /// lines are aliasing the tokens in the expanded token stream, so that token
29753f127fSDimitry Andric /// annotations will be reused when formatting the spelled macro calls.
30e8d8bef9SDimitry Andric ///
31753f127fSDimitry Andric /// When formatting, clang-format annotates and formats the expanded unwrapped
32753f127fSDimitry Andric /// lines first, determining the token types. Next, it formats the spelled
33753f127fSDimitry Andric /// unwrapped lines, keeping the token types fixed, while allowing other
34753f127fSDimitry Andric /// formatting decisions to change.
35e8d8bef9SDimitry Andric ///
36e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
37e8d8bef9SDimitry Andric 
38e8d8bef9SDimitry Andric #ifndef CLANG_LIB_FORMAT_MACROS_H
39e8d8bef9SDimitry Andric #define CLANG_LIB_FORMAT_MACROS_H
40e8d8bef9SDimitry Andric 
41753f127fSDimitry Andric #include <list>
42e8d8bef9SDimitry Andric 
43e8d8bef9SDimitry Andric #include "FormatToken.h"
44753f127fSDimitry Andric #include "llvm/ADT/DenseMap.h"
45e8d8bef9SDimitry Andric 
46e8d8bef9SDimitry Andric namespace clang {
47e8d8bef9SDimitry Andric namespace format {
48753f127fSDimitry Andric 
49753f127fSDimitry Andric struct UnwrappedLine;
50753f127fSDimitry Andric struct UnwrappedLineNode;
51e8d8bef9SDimitry Andric 
52e8d8bef9SDimitry Andric /// Takes a set of macro definitions as strings and allows expanding calls to
53e8d8bef9SDimitry Andric /// those macros.
54e8d8bef9SDimitry Andric ///
55e8d8bef9SDimitry Andric /// For example:
56e8d8bef9SDimitry Andric /// Definition: A(x, y)=x + y
57e8d8bef9SDimitry Andric /// Call      : A(int a = 1, 2)
58e8d8bef9SDimitry Andric /// Expansion : int a = 1 + 2
59e8d8bef9SDimitry Andric ///
60e8d8bef9SDimitry Andric /// Expansion does not check arity of the definition.
61e8d8bef9SDimitry Andric /// If fewer arguments than expected are provided, the remaining parameters
62e8d8bef9SDimitry Andric /// are considered empty:
63e8d8bef9SDimitry Andric /// Call     : A(a)
64e8d8bef9SDimitry Andric /// Expansion: a +
65e8d8bef9SDimitry Andric /// If more arguments than expected are provided, they will be discarded.
66e8d8bef9SDimitry Andric ///
67e8d8bef9SDimitry Andric /// The expander does not support:
68e8d8bef9SDimitry Andric /// - recursive expansion
69e8d8bef9SDimitry Andric /// - stringification
70e8d8bef9SDimitry Andric /// - concatenation
71e8d8bef9SDimitry Andric /// - variadic macros
72e8d8bef9SDimitry Andric ///
73e8d8bef9SDimitry Andric /// Furthermore, only a single expansion of each macro argument is supported,
74e8d8bef9SDimitry Andric /// so that we cannot get conflicting formatting decisions from different
75e8d8bef9SDimitry Andric /// expansions.
76e8d8bef9SDimitry Andric /// Definition: A(x)=x+x
77e8d8bef9SDimitry Andric /// Call      : A(id)
78e8d8bef9SDimitry Andric /// Expansion : id+x
79e8d8bef9SDimitry Andric ///
80e8d8bef9SDimitry Andric class MacroExpander {
81e8d8bef9SDimitry Andric public:
82*0fca6ea1SDimitry Andric   using ArgsList = ArrayRef<SmallVector<FormatToken *, 8>>;
83e8d8bef9SDimitry Andric 
84e8d8bef9SDimitry Andric   /// Construct a macro expander from a set of macro definitions.
85e8d8bef9SDimitry Andric   /// Macro definitions must be encoded as UTF-8.
86e8d8bef9SDimitry Andric   ///
87e8d8bef9SDimitry Andric   /// Each entry in \p Macros must conform to the following simple
88e8d8bef9SDimitry Andric   /// macro-definition language:
89e8d8bef9SDimitry Andric   /// <definition> ::= <id> <expansion> | <id> "(" <params> ")" <expansion>
90e8d8bef9SDimitry Andric   /// <params>     ::= <id-list> | ""
91e8d8bef9SDimitry Andric   /// <id-list>    ::= <id> | <id> "," <params>
92e8d8bef9SDimitry Andric   /// <expansion>  ::= "=" <tail> | <eof>
93e8d8bef9SDimitry Andric   /// <tail>       ::= <tok> <tail> | <eof>
94e8d8bef9SDimitry Andric   ///
95e8d8bef9SDimitry Andric   /// Macros that cannot be parsed will be silently discarded.
96e8d8bef9SDimitry Andric   ///
97e8d8bef9SDimitry Andric   MacroExpander(const std::vector<std::string> &Macros,
98*0fca6ea1SDimitry Andric                 SourceManager &SourceMgr, const FormatStyle &Style,
99e8d8bef9SDimitry Andric                 llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
100e8d8bef9SDimitry Andric                 IdentifierTable &IdentTable);
101e8d8bef9SDimitry Andric   ~MacroExpander();
102e8d8bef9SDimitry Andric 
10306c3fb27SDimitry Andric   /// Returns whether any macro \p Name is defined, regardless of overloads.
104*0fca6ea1SDimitry Andric   bool defined(StringRef Name) const;
105e8d8bef9SDimitry Andric 
10606c3fb27SDimitry Andric   /// Returns whetherh there is an object-like overload, i.e. where the macro
10706c3fb27SDimitry Andric   /// has no arguments and should not consume subsequent parentheses.
108*0fca6ea1SDimitry Andric   bool objectLike(StringRef Name) const;
109e8d8bef9SDimitry Andric 
11006c3fb27SDimitry Andric   /// Returns whether macro \p Name provides an overload with the given arity.
111*0fca6ea1SDimitry Andric   bool hasArity(StringRef Name, unsigned Arity) const;
11206c3fb27SDimitry Andric 
113e8d8bef9SDimitry Andric   /// Returns the expanded stream of format tokens for \p ID, where
114e8d8bef9SDimitry Andric   /// each element in \p Args is a positional argument to the macro call.
11506c3fb27SDimitry Andric   /// If \p Args is not set, the object-like overload is used.
11606c3fb27SDimitry Andric   /// If \p Args is set, the overload with the arity equal to \c Args.size() is
11706c3fb27SDimitry Andric   /// used.
118*0fca6ea1SDimitry Andric   SmallVector<FormatToken *, 8>
11906c3fb27SDimitry Andric   expand(FormatToken *ID, std::optional<ArgsList> OptionalArgs) const;
120e8d8bef9SDimitry Andric 
121e8d8bef9SDimitry Andric private:
122e8d8bef9SDimitry Andric   struct Definition;
123e8d8bef9SDimitry Andric   class DefinitionParser;
124e8d8bef9SDimitry Andric 
125e8d8bef9SDimitry Andric   void parseDefinition(const std::string &Macro);
126e8d8bef9SDimitry Andric 
127*0fca6ea1SDimitry Andric   SourceManager &SourceMgr;
128e8d8bef9SDimitry Andric   const FormatStyle &Style;
129e8d8bef9SDimitry Andric   llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator;
130e8d8bef9SDimitry Andric   IdentifierTable &IdentTable;
131753f127fSDimitry Andric   SmallVector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
13206c3fb27SDimitry Andric   llvm::StringMap<llvm::DenseMap<int, Definition>> FunctionLike;
13306c3fb27SDimitry Andric   llvm::StringMap<Definition> ObjectLike;
134e8d8bef9SDimitry Andric };
135e8d8bef9SDimitry Andric 
136753f127fSDimitry Andric /// Converts a sequence of UnwrappedLines containing expanded macros into a
137753f127fSDimitry Andric /// single UnwrappedLine containing the macro calls.  This UnwrappedLine may be
138753f127fSDimitry Andric /// broken into child lines, in a way that best conveys the structure of the
139753f127fSDimitry Andric /// expanded code.
140753f127fSDimitry Andric ///
141753f127fSDimitry Andric /// In the simplest case, a spelled UnwrappedLine contains one macro, and after
142753f127fSDimitry Andric /// expanding it we have one expanded UnwrappedLine.  In general, macro
143753f127fSDimitry Andric /// expansions can span UnwrappedLines, and multiple macros can contribute
144753f127fSDimitry Andric /// tokens to the same line.  We keep consuming expanded lines until:
145753f127fSDimitry Andric /// *   all expansions that started have finished (we're not chopping any macros
146753f127fSDimitry Andric ///     in half)
147753f127fSDimitry Andric /// *   *and* we've reached the end of a *spelled* unwrapped line.
148753f127fSDimitry Andric ///
149753f127fSDimitry Andric /// A single UnwrappedLine represents this chunk of code.
150753f127fSDimitry Andric ///
151753f127fSDimitry Andric /// After this point, the state of the spelled/expanded stream is "in sync"
152753f127fSDimitry Andric /// (both at the start of an UnwrappedLine, with no macros open), so the
15306c3fb27SDimitry Andric /// Reconstructor can be thrown away and parsing can continue.
154753f127fSDimitry Andric ///
155753f127fSDimitry Andric /// Given a mapping from the macro name identifier token in the macro call
156753f127fSDimitry Andric /// to the tokens of the macro call, for example:
157753f127fSDimitry Andric /// CLASSA -> CLASSA({public: void x();})
158753f127fSDimitry Andric ///
159753f127fSDimitry Andric /// When getting the formatted lines of the expansion via the \c addLine method
160753f127fSDimitry Andric /// (each '->' specifies a call to \c addLine ):
161753f127fSDimitry Andric /// -> class A {
162753f127fSDimitry Andric /// -> public:
163753f127fSDimitry Andric /// ->   void x();
164753f127fSDimitry Andric /// -> };
165753f127fSDimitry Andric ///
166753f127fSDimitry Andric /// Creates the tree of unwrapped lines containing the macro call tokens so that
167753f127fSDimitry Andric /// the macro call tokens fit the semantic structure of the expanded formatted
168753f127fSDimitry Andric /// lines:
169753f127fSDimitry Andric /// -> CLASSA({
170753f127fSDimitry Andric /// -> public:
171753f127fSDimitry Andric /// ->   void x();
172753f127fSDimitry Andric /// -> })
173753f127fSDimitry Andric class MacroCallReconstructor {
174753f127fSDimitry Andric public:
175753f127fSDimitry Andric   /// Create an Reconstructor whose resulting \p UnwrappedLine will start at
176753f127fSDimitry Andric   /// \p Level, using the map from name identifier token to the corresponding
177753f127fSDimitry Andric   /// tokens of the spelled macro call.
178753f127fSDimitry Andric   MacroCallReconstructor(
179753f127fSDimitry Andric       unsigned Level,
180753f127fSDimitry Andric       const llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>>
181753f127fSDimitry Andric           &ActiveExpansions);
182753f127fSDimitry Andric 
183753f127fSDimitry Andric   /// For the given \p Line, match all occurences of tokens expanded from a
184753f127fSDimitry Andric   /// macro to unwrapped lines in the spelled macro call so that the resulting
185753f127fSDimitry Andric   /// tree of unwrapped lines best resembles the structure of unwrapped lines
186753f127fSDimitry Andric   /// passed in via \c addLine.
187753f127fSDimitry Andric   void addLine(const UnwrappedLine &Line);
188753f127fSDimitry Andric 
189753f127fSDimitry Andric   /// Check whether at the current state there is no open macro expansion
190753f127fSDimitry Andric   /// that needs to be processed to finish an macro call.
191753f127fSDimitry Andric   /// Only when \c finished() is true, \c takeResult() can be called to retrieve
192753f127fSDimitry Andric   /// the resulting \c UnwrappedLine.
193753f127fSDimitry Andric   /// If there are multiple subsequent macro calls within an unwrapped line in
194753f127fSDimitry Andric   /// the spelled token stream, the calling code may also continue to call
195753f127fSDimitry Andric   /// \c addLine() when \c finished() is true.
196753f127fSDimitry Andric   bool finished() const { return ActiveExpansions.empty(); }
197753f127fSDimitry Andric 
198753f127fSDimitry Andric   /// Retrieve the formatted \c UnwrappedLine containing the orginal
199753f127fSDimitry Andric   /// macro calls, formatted according to the expanded token stream received
200753f127fSDimitry Andric   /// via \c addLine().
201753f127fSDimitry Andric   /// Generally, this line tries to have the same structure as the expanded,
202753f127fSDimitry Andric   /// formatted unwrapped lines handed in via \c addLine(), with the exception
203753f127fSDimitry Andric   /// that for multiple top-level lines, each subsequent line will be the
204753f127fSDimitry Andric   /// child of the last token in its predecessor. This representation is chosen
205753f127fSDimitry Andric   /// because it is a precondition to the formatter that we get what looks like
206753f127fSDimitry Andric   /// a single statement in a single \c UnwrappedLine (i.e. matching parens).
207753f127fSDimitry Andric   ///
208753f127fSDimitry Andric   /// If a token in a macro argument is a child of a token in the expansion,
209753f127fSDimitry Andric   /// the parent will be the corresponding token in the macro call.
210753f127fSDimitry Andric   /// For example:
211753f127fSDimitry Andric   ///   #define C(a, b) class C { a b
212753f127fSDimitry Andric   ///   C(int x;, int y;)
213753f127fSDimitry Andric   /// would expand to
214753f127fSDimitry Andric   ///   class C { int x; int y;
215753f127fSDimitry Andric   /// where in a formatted line "int x;" and "int y;" would both be new separate
216753f127fSDimitry Andric   /// lines.
217753f127fSDimitry Andric   ///
218753f127fSDimitry Andric   /// In the result, "int x;" will be a child of the opening parenthesis in "C("
219753f127fSDimitry Andric   /// and "int y;" will be a child of the "," token:
220753f127fSDimitry Andric   ///   C (
221753f127fSDimitry Andric   ///     \- int x;
222753f127fSDimitry Andric   ///     ,
223753f127fSDimitry Andric   ///     \- int y;
224753f127fSDimitry Andric   ///     )
225753f127fSDimitry Andric   UnwrappedLine takeResult() &&;
226753f127fSDimitry Andric 
227753f127fSDimitry Andric private:
228*0fca6ea1SDimitry Andric   void add(FormatToken *Token, FormatToken *ExpandedParent, bool First,
229*0fca6ea1SDimitry Andric            unsigned Level);
230*0fca6ea1SDimitry Andric   void prepareParent(FormatToken *ExpandedParent, bool First, unsigned Level);
231753f127fSDimitry Andric   FormatToken *getParentInResult(FormatToken *Parent);
232753f127fSDimitry Andric   void reconstruct(FormatToken *Token);
233753f127fSDimitry Andric   void startReconstruction(FormatToken *Token);
234753f127fSDimitry Andric   bool reconstructActiveCallUntil(FormatToken *Token);
235753f127fSDimitry Andric   void endReconstruction(FormatToken *Token);
236753f127fSDimitry Andric   bool processNextReconstructed();
237753f127fSDimitry Andric   void finalize();
238753f127fSDimitry Andric 
239753f127fSDimitry Andric   struct ReconstructedLine;
240753f127fSDimitry Andric 
241753f127fSDimitry Andric   void appendToken(FormatToken *Token, ReconstructedLine *L = nullptr);
242753f127fSDimitry Andric   UnwrappedLine createUnwrappedLine(const ReconstructedLine &Line, int Level);
243753f127fSDimitry Andric   void debug(const ReconstructedLine &Line, int Level);
244753f127fSDimitry Andric   ReconstructedLine &parentLine();
245753f127fSDimitry Andric   ReconstructedLine *currentLine();
246753f127fSDimitry Andric   void debugParentMap() const;
247753f127fSDimitry Andric 
248753f127fSDimitry Andric #ifndef NDEBUG
249753f127fSDimitry Andric   enum ReconstructorState {
250753f127fSDimitry Andric     Start,      // No macro expansion was found in the input yet.
251753f127fSDimitry Andric     InProgress, // During a macro reconstruction.
252753f127fSDimitry Andric     Finalized,  // Past macro reconstruction, the result is finalized.
253753f127fSDimitry Andric   };
254753f127fSDimitry Andric   ReconstructorState State = Start;
255753f127fSDimitry Andric #endif
256753f127fSDimitry Andric 
257753f127fSDimitry Andric   // Node in which we build up the resulting unwrapped line; this type is
258753f127fSDimitry Andric   // analogous to UnwrappedLineNode.
259753f127fSDimitry Andric   struct LineNode {
260753f127fSDimitry Andric     LineNode() = default;
261753f127fSDimitry Andric     LineNode(FormatToken *Tok) : Tok(Tok) {}
262753f127fSDimitry Andric     FormatToken *Tok = nullptr;
263*0fca6ea1SDimitry Andric     SmallVector<std::unique_ptr<ReconstructedLine>> Children;
264753f127fSDimitry Andric   };
265753f127fSDimitry Andric 
266753f127fSDimitry Andric   // Line in which we build up the resulting unwrapped line.
267753f127fSDimitry Andric   // FIXME: Investigate changing UnwrappedLine to a pointer type and using it
268753f127fSDimitry Andric   // instead of rolling our own type.
269753f127fSDimitry Andric   struct ReconstructedLine {
270*0fca6ea1SDimitry Andric     explicit ReconstructedLine(unsigned Level) : Level(Level) {}
271*0fca6ea1SDimitry Andric     unsigned Level;
272*0fca6ea1SDimitry Andric     SmallVector<std::unique_ptr<LineNode>> Tokens;
273753f127fSDimitry Andric   };
274753f127fSDimitry Andric 
275753f127fSDimitry Andric   // The line in which we collect the resulting reconstructed output.
276753f127fSDimitry Andric   // To reduce special cases in the algorithm, the first level of the line
277753f127fSDimitry Andric   // contains a single null token that has the reconstructed incoming
278753f127fSDimitry Andric   // lines as children.
279753f127fSDimitry Andric   // In the end, we stich the lines together so that each subsequent line
280753f127fSDimitry Andric   // is a child of the last token of the previous line. This is necessary
281753f127fSDimitry Andric   // in order to format the overall expression as a single logical line -
282753f127fSDimitry Andric   // if we created separate lines, we'd format them with their own top-level
283753f127fSDimitry Andric   // indent depending on the semantic structure, which is not desired.
284753f127fSDimitry Andric   ReconstructedLine Result;
285753f127fSDimitry Andric 
286753f127fSDimitry Andric   // Stack of currently "open" lines, where each line's predecessor's last
287753f127fSDimitry Andric   // token is the parent token for that line.
288*0fca6ea1SDimitry Andric   SmallVector<ReconstructedLine *> ActiveReconstructedLines;
289753f127fSDimitry Andric 
290753f127fSDimitry Andric   // Maps from the expanded token to the token that takes its place in the
291753f127fSDimitry Andric   // reconstructed token stream in terms of parent-child relationships.
292753f127fSDimitry Andric   // Note that it might take multiple steps to arrive at the correct
293753f127fSDimitry Andric   // parent in the output.
294753f127fSDimitry Andric   // Given: #define C(a, b) []() { a; b; }
295753f127fSDimitry Andric   // And a call: C(f(), g())
296753f127fSDimitry Andric   // The structure in the incoming formatted unwrapped line will be:
297753f127fSDimitry Andric   // []() {
298753f127fSDimitry Andric   //      |- f();
299753f127fSDimitry Andric   //      \- g();
300753f127fSDimitry Andric   // }
301753f127fSDimitry Andric   // with f and g being children of the opening brace.
302753f127fSDimitry Andric   // In the reconstructed call:
303753f127fSDimitry Andric   // C(f(), g())
304753f127fSDimitry Andric   //  \- f()
305753f127fSDimitry Andric   //      \- g()
306753f127fSDimitry Andric   // We want f to be a child of the opening parenthesis and g to be a child
307753f127fSDimitry Andric   // of the comma token in the macro call.
308753f127fSDimitry Andric   // Thus, we map
309753f127fSDimitry Andric   // { -> (
310753f127fSDimitry Andric   // and add
311753f127fSDimitry Andric   // ( -> ,
312753f127fSDimitry Andric   // once we're past the comma in the reconstruction.
313753f127fSDimitry Andric   llvm::DenseMap<FormatToken *, FormatToken *>
314753f127fSDimitry Andric       SpelledParentToReconstructedParent;
315753f127fSDimitry Andric 
316753f127fSDimitry Andric   // Keeps track of a single expansion while we're reconstructing tokens it
317753f127fSDimitry Andric   // generated.
318753f127fSDimitry Andric   struct Expansion {
319753f127fSDimitry Andric     // The identifier token of the macro call.
320753f127fSDimitry Andric     FormatToken *ID;
321753f127fSDimitry Andric     // Our current position in the reconstruction.
322753f127fSDimitry Andric     std::list<UnwrappedLineNode>::iterator SpelledI;
323753f127fSDimitry Andric     // The end of the reconstructed token sequence.
324753f127fSDimitry Andric     std::list<UnwrappedLineNode>::iterator SpelledE;
325753f127fSDimitry Andric   };
326753f127fSDimitry Andric 
327753f127fSDimitry Andric   // Stack of macro calls for which we're in the middle of an expansion.
328*0fca6ea1SDimitry Andric   SmallVector<Expansion> ActiveExpansions;
329753f127fSDimitry Andric 
330753f127fSDimitry Andric   struct MacroCallState {
331753f127fSDimitry Andric     MacroCallState(ReconstructedLine *Line, FormatToken *ParentLastToken,
332753f127fSDimitry Andric                    FormatToken *MacroCallLParen);
333753f127fSDimitry Andric 
334753f127fSDimitry Andric     ReconstructedLine *Line;
335753f127fSDimitry Andric 
336753f127fSDimitry Andric     // The last token in the parent line or expansion, or nullptr if the macro
337753f127fSDimitry Andric     // expansion is on a top-level line.
338753f127fSDimitry Andric     //
339753f127fSDimitry Andric     // For example, in the macro call:
340753f127fSDimitry Andric     //   auto f = []() { ID(1); };
341753f127fSDimitry Andric     // The MacroCallState for ID will have '{' as ParentLastToken.
342753f127fSDimitry Andric     //
343753f127fSDimitry Andric     // In the macro call:
344753f127fSDimitry Andric     //   ID(ID(void f()));
345753f127fSDimitry Andric     // The MacroCallState of the outer ID will have nullptr as ParentLastToken,
346753f127fSDimitry Andric     // while the MacroCallState for the inner ID will have the '(' of the outer
347753f127fSDimitry Andric     // ID as ParentLastToken.
348753f127fSDimitry Andric     //
349753f127fSDimitry Andric     // In the macro call:
350753f127fSDimitry Andric     //   ID2(a, ID(b));
351753f127fSDimitry Andric     // The MacroCallState of ID will have ',' as ParentLastToken.
352753f127fSDimitry Andric     FormatToken *ParentLastToken;
353753f127fSDimitry Andric 
354753f127fSDimitry Andric     // The l_paren of this MacroCallState's macro call.
355753f127fSDimitry Andric     FormatToken *MacroCallLParen;
356753f127fSDimitry Andric   };
357753f127fSDimitry Andric 
358753f127fSDimitry Andric   // Keeps track of the lines into which the opening brace/parenthesis &
359753f127fSDimitry Andric   // argument separating commas for each level in the macro call go in order to
360753f127fSDimitry Andric   // put the corresponding closing brace/parenthesis into the same line in the
361753f127fSDimitry Andric   // output and keep track of which parents in the expanded token stream map to
362753f127fSDimitry Andric   // which tokens in the reconstructed stream.
363753f127fSDimitry Andric   // When an opening brace/parenthesis has children, we want the structure of
364753f127fSDimitry Andric   // the output line to be:
365753f127fSDimitry Andric   // |- MACRO
366753f127fSDimitry Andric   // |- (
367753f127fSDimitry Andric   // |  \- <argument>
368753f127fSDimitry Andric   // |- ,
369753f127fSDimitry Andric   // |  \- <argument>
370753f127fSDimitry Andric   // \- )
371*0fca6ea1SDimitry Andric   SmallVector<MacroCallState> MacroCallStructure;
372753f127fSDimitry Andric 
373753f127fSDimitry Andric   // Maps from identifier of the macro call to an unwrapped line containing
374753f127fSDimitry Andric   // all tokens of the macro call.
375753f127fSDimitry Andric   const llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>>
376753f127fSDimitry Andric       &IdToReconstructed;
377753f127fSDimitry Andric };
378753f127fSDimitry Andric 
379e8d8bef9SDimitry Andric } // namespace format
380e8d8bef9SDimitry Andric } // namespace clang
381e8d8bef9SDimitry Andric 
382e8d8bef9SDimitry Andric #endif
383