llvm-project
240 строк · 7.3 Кб
1//===--- MacroExpander.cpp - Format C++ code --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the implementation of MacroExpander, which handles macro
11/// configuration and expansion while formatting.
12///
13//===----------------------------------------------------------------------===//
14
15#include "Macros.h"
16
17#include "Encoding.h"
18#include "FormatToken.h"
19#include "FormatTokenLexer.h"
20#include "clang/Basic/TokenKinds.h"
21#include "clang/Format/Format.h"
22#include "clang/Lex/HeaderSearch.h"
23#include "clang/Lex/HeaderSearchOptions.h"
24#include "clang/Lex/Lexer.h"
25#include "clang/Lex/ModuleLoader.h"
26#include "clang/Lex/Preprocessor.h"
27#include "clang/Lex/PreprocessorOptions.h"
28#include "llvm/ADT/StringSet.h"
29#include "llvm/Support/ErrorHandling.h"
30
31namespace clang {
32namespace format {
33
34struct MacroExpander::Definition {
35StringRef Name;
36SmallVector<FormatToken *, 8> Params;
37SmallVector<FormatToken *, 8> Body;
38
39// Map from each argument's name to its position in the argument list.
40// With "M(x, y) x + y":
41// x -> 0
42// y -> 1
43llvm::StringMap<size_t> ArgMap;
44
45bool ObjectLike = true;
46};
47
48class MacroExpander::DefinitionParser {
49public:
50DefinitionParser(ArrayRef<FormatToken *> Tokens) : Tokens(Tokens) {
51assert(!Tokens.empty());
52Current = Tokens[0];
53}
54
55// Parse the token stream and return the corresponding Definition object.
56// Returns an empty definition object with a null-Name on error.
57MacroExpander::Definition parse() {
58if (Current->isNot(tok::identifier))
59return {};
60Def.Name = Current->TokenText;
61nextToken();
62if (Current->is(tok::l_paren)) {
63Def.ObjectLike = false;
64if (!parseParams())
65return {};
66}
67if (!parseExpansion())
68return {};
69
70return Def;
71}
72
73private:
74bool parseParams() {
75assert(Current->is(tok::l_paren));
76nextToken();
77while (Current->is(tok::identifier)) {
78Def.Params.push_back(Current);
79Def.ArgMap[Def.Params.back()->TokenText] = Def.Params.size() - 1;
80nextToken();
81if (Current->isNot(tok::comma))
82break;
83nextToken();
84}
85if (Current->isNot(tok::r_paren))
86return false;
87nextToken();
88return true;
89}
90
91bool parseExpansion() {
92if (!Current->isOneOf(tok::equal, tok::eof))
93return false;
94if (Current->is(tok::equal))
95nextToken();
96parseTail();
97return true;
98}
99
100void parseTail() {
101while (Current->isNot(tok::eof)) {
102Def.Body.push_back(Current);
103nextToken();
104}
105Def.Body.push_back(Current);
106}
107
108void nextToken() {
109if (Pos + 1 < Tokens.size())
110++Pos;
111Current = Tokens[Pos];
112Current->Finalized = true;
113}
114
115size_t Pos = 0;
116FormatToken *Current = nullptr;
117Definition Def;
118ArrayRef<FormatToken *> Tokens;
119};
120
121MacroExpander::MacroExpander(
122const std::vector<std::string> &Macros, SourceManager &SourceMgr,
123const FormatStyle &Style,
124llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
125IdentifierTable &IdentTable)
126: SourceMgr(SourceMgr), Style(Style), Allocator(Allocator),
127IdentTable(IdentTable) {
128for (const std::string &Macro : Macros)
129parseDefinition(Macro);
130}
131
132MacroExpander::~MacroExpander() = default;
133
134void MacroExpander::parseDefinition(const std::string &Macro) {
135Buffers.push_back(
136llvm::MemoryBuffer::getMemBufferCopy(Macro, "<scratch space>"));
137FileID FID = SourceMgr.createFileID(Buffers.back()->getMemBufferRef());
138FormatTokenLexer Lex(SourceMgr, FID, 0, Style, encoding::Encoding_UTF8,
139Allocator, IdentTable);
140const auto Tokens = Lex.lex();
141if (!Tokens.empty()) {
142DefinitionParser Parser(Tokens);
143auto Definition = Parser.parse();
144if (Definition.ObjectLike) {
145ObjectLike[Definition.Name] = std::move(Definition);
146} else {
147FunctionLike[Definition.Name][Definition.Params.size()] =
148std::move(Definition);
149}
150}
151}
152
153bool MacroExpander::defined(StringRef Name) const {
154return FunctionLike.contains(Name) || ObjectLike.contains(Name);
155}
156
157bool MacroExpander::objectLike(StringRef Name) const {
158return ObjectLike.contains(Name);
159}
160
161bool MacroExpander::hasArity(StringRef Name, unsigned Arity) const {
162auto it = FunctionLike.find(Name);
163return it != FunctionLike.end() && it->second.contains(Arity);
164}
165
166SmallVector<FormatToken *, 8>
167MacroExpander::expand(FormatToken *ID,
168std::optional<ArgsList> OptionalArgs) const {
169if (OptionalArgs)
170assert(hasArity(ID->TokenText, OptionalArgs->size()));
171else
172assert(objectLike(ID->TokenText));
173const Definition &Def = OptionalArgs
174? FunctionLike.find(ID->TokenText)
175->second.find(OptionalArgs.value().size())
176->second
177: ObjectLike.find(ID->TokenText)->second;
178ArgsList Args = OptionalArgs ? OptionalArgs.value() : ArgsList();
179SmallVector<FormatToken *, 8> Result;
180// Expand each argument at most once.
181llvm::StringSet<> ExpandedArgs;
182
183// Adds the given token to Result.
184auto pushToken = [&](FormatToken *Tok) {
185Tok->MacroCtx->ExpandedFrom.push_back(ID);
186Result.push_back(Tok);
187};
188
189// If Tok references a parameter, adds the corresponding argument to Result.
190// Returns false if Tok does not reference a parameter.
191auto expandArgument = [&](FormatToken *Tok) -> bool {
192// If the current token references a parameter, expand the corresponding
193// argument.
194if (Tok->isNot(tok::identifier) || ExpandedArgs.contains(Tok->TokenText))
195return false;
196ExpandedArgs.insert(Tok->TokenText);
197auto I = Def.ArgMap.find(Tok->TokenText);
198if (I == Def.ArgMap.end())
199return false;
200// If there are fewer arguments than referenced parameters, treat the
201// parameter as empty.
202// FIXME: Potentially fully abort the expansion instead.
203if (I->getValue() >= Args.size())
204return true;
205for (FormatToken *Arg : Args[I->getValue()]) {
206// A token can be part of a macro argument at multiple levels.
207// For example, with "ID(x) x":
208// in ID(ID(x)), 'x' is expanded first as argument to the inner
209// ID, then again as argument to the outer ID. We keep the macro
210// role the token had from the inner expansion.
211if (!Arg->MacroCtx)
212Arg->MacroCtx = MacroExpansion(MR_ExpandedArg);
213pushToken(Arg);
214}
215return true;
216};
217
218// Expand the definition into Result.
219for (FormatToken *Tok : Def.Body) {
220if (expandArgument(Tok))
221continue;
222// Create a copy of the tokens from the macro body, i.e. were not provided
223// by user code.
224FormatToken *New = new (Allocator.Allocate()) FormatToken;
225New->copyFrom(*Tok);
226assert(!New->MacroCtx);
227// Tokens that are not part of the user code are not formatted.
228New->MacroCtx = MacroExpansion(MR_Hidden);
229pushToken(New);
230}
231assert(Result.size() >= 1 && Result.back()->is(tok::eof));
232if (Result.size() > 1) {
233++Result[0]->MacroCtx->StartOfExpansion;
234++Result[Result.size() - 2]->MacroCtx->EndOfExpansion;
235}
236return Result;
237}
238
239} // namespace format
240} // namespace clang
241