clang 24.0.0git
IncrementalParser.cpp
Go to the documentation of this file.
1//===--------- IncrementalParser.cpp - Incremental Compilation -----------===//
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// This file implements the class which performs incremental code compilation.
10//
11//===----------------------------------------------------------------------===//
12
13#include "IncrementalParser.h"
14#include "IncrementalAction.h"
15
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclCXX.h"
24#include "clang/Parse/Parser.h"
25#include "clang/Sema/Sema.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/IR/Module.h"
28#include "llvm/Support/CrashRecoveryContext.h"
29#include "llvm/Support/Error.h"
30
31#include <sstream>
32
33#define DEBUG_TYPE "clang-repl"
34
35namespace clang {
36
37// IncrementalParser::IncrementalParser() {}
38
40 IncrementalAction *Act, llvm::Error &Err,
41 std::list<PartialTranslationUnit> &PTUs)
42 : S(Instance.getSema()), Act(Act), PTUs(PTUs) {
43 llvm::ErrorAsOutParameter EAO(&Err);
44 Consumer = &S.getASTConsumer();
45 P.reset(new Parser(S.getPreprocessor(), S, /*SkipBodies=*/false));
46
47 if (ExternalASTSource *External = S.getASTContext().getExternalSource())
48 External->StartTranslationUnit(Consumer);
49
50 P->Initialize();
51}
52
54
56IncrementalParser::ParseOrWrapTopLevelDecl() {
57 // Recover resources if we crash before exiting this method.
58 llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(&S);
59 Sema::GlobalEagerInstantiationScope GlobalInstantiations(S, /*Enabled=*/true,
60 /*AtEndOfTU=*/true);
61 Sema::LocalEagerInstantiationScope LocalInstantiations(S, /*AtEndOfTU=*/true);
62
63 // Add a new PTU.
65 C.addTranslationUnitDecl();
66
67 // Skip previous eof due to last incremental input.
68 if (P->getCurToken().is(tok::eof)) {
69 P->ConsumeAnyToken();
70 // FIXME: Clang does not call ExitScope on finalizing the regular TU, we
71 // might want to do that around HandleEndOfTranslationUnit.
72 P->ExitScope();
73 S.CurContext = nullptr;
74 // Start a new PTU.
75 P->EnterScope(Scope::DeclScope);
76 S.ActOnTranslationUnitScope(P->getCurScope());
77 }
78
80 Sema::ModuleImportState ImportState;
81 for (bool AtEOF = P->ParseFirstTopLevelDecl(ADecl, ImportState); !AtEOF;
82 AtEOF = P->ParseTopLevelDecl(ADecl, ImportState)) {
83 if (ADecl && !Consumer->HandleTopLevelDecl(ADecl.get()))
84 return llvm::make_error<llvm::StringError>("Parsing failed. "
85 "The consumer rejected a decl",
86 std::error_code());
87 }
88
89 DiagnosticsEngine &Diags = S.getDiagnostics();
90 if (Diags.hasErrorOccurred()) {
91 CleanUpPTU(C.getTranslationUnitDecl());
92
93 Diags.Reset(/*soft=*/true);
94 Diags.getClient()->clear();
95 return llvm::make_error<llvm::StringError>("Parsing failed.",
96 std::error_code());
97 }
98
99 // Process any TopLevelDecls generated by #pragma weak.
100 for (Decl *D : S.WeakTopLevelDecls()) {
101 DeclGroupRef DGR(D);
102 Consumer->HandleTopLevelDecl(DGR);
103 }
104
105 LocalInstantiations.perform();
106 GlobalInstantiations.perform();
107
108 Consumer->HandleTranslationUnit(C);
109
110 return C.getTranslationUnitDecl();
111}
112
114IncrementalParser::Parse(llvm::StringRef input) {
115 Preprocessor &PP = S.getPreprocessor();
116 assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode!?");
117
118 std::ostringstream SourceName;
119 SourceName << "input_line_" << InputCount++;
120
121 // Create an uninitialized memory buffer, copy code in and append "\n"
122 size_t InputSize = input.size(); // don't include trailing 0
123 // MemBuffer size should *not* include terminating zero
124 std::unique_ptr<llvm::MemoryBuffer> MB(
125 llvm::WritableMemoryBuffer::getNewUninitMemBuffer(InputSize + 1,
126 SourceName.str()));
127 char *MBStart = const_cast<char *>(MB->getBufferStart());
128 memcpy(MBStart, input.data(), InputSize);
129 MBStart[InputSize] = '\n';
130
131 SourceManager &SM = S.getSourceManager();
132
133 // FIXME: Create SourceLocation, which will allow clang to order the overload
134 // candidates for example
136
137 // Create FileID for the current buffer.
138 FileID FID;
139 // Create FileEntry and FileID for the current buffer.
141 SourceName.str(), InputSize, 0 /* mod time*/);
142 SM.overrideFileContents(FE, std::move(MB));
143
144 // Ensure HeaderFileInfo exists before lookup to prevent assertion
146 HS.getFileInfo(FE);
147
148 FID = SM.createFileID(FE, NewLoc, SrcMgr::C_User);
149
150 // NewLoc only used for diags.
151 if (PP.EnterSourceFile(FID, /*DirLookup=*/nullptr, NewLoc))
152 return llvm::make_error<llvm::StringError>("Parsing failed. "
153 "Cannot enter source file.",
154 std::error_code());
155
156 auto PTU = ParseOrWrapTopLevelDecl();
157 if (!PTU)
158 return PTU.takeError();
159
160 if (PP.getLangOpts().DelayedTemplateParsing) {
161 // Microsoft-specific:
162 // Late parsed templates can leave unswallowed "macro"-like tokens.
163 // They will seriously confuse the Parser when entering the next
164 // source file. So lex until we are EOF.
165 Token Tok;
166 do {
167 PP.Lex(Tok);
168 } while (Tok.isNot(tok::eof));
169 } else {
170 Token AssertTok;
171 PP.Lex(AssertTok);
172 assert(AssertTok.is(tok::eof) &&
173 "Lexer must be EOF when starting incremental parse!");
174 }
175
176 return PTU;
177}
178
179void IncrementalParser::withdrawMostRecentTU(
180 TranslationUnitDecl *MostRecentTU) {
181 TranslationUnitDecl *Prev = MostRecentTU->getPreviousDecl();
182 if (!Prev)
183 return;
184 assert(MostRecentTU->getMostRecentDecl() == MostRecentTU &&
185 "Not the most recent translation unit!");
186
187 // Rebuild A -> ... -> Prev -> MostRecentTU as A -> ... -> Prev.
188 MostRecentTU->getFirstDecl()->RedeclLink.setLatest(Prev);
189
190 // getTranslationUnitDecl() requires the active unit to be the latest one.
192 if (C.TraversalScope.size() == 1 && C.TraversalScope.back() == MostRecentTU)
193 C.TraversalScope = {Prev};
194 C.TUDecl = Prev;
195}
196
197/// Removes decls introduced in the discarding PTU and restores the
198/// redeclaration chain to previous state.
199class ASTDeclUnmerger : public DeclVisitor<ASTDeclUnmerger> {
200 Sema &S;
201 TranslationUnitDecl *DiscardedTU;
202
203 /// The newest declaration of whatever D redeclares that still lives outside
204 /// the DiscardedTU, or null if DiscardedTU introduced the name.
205 NamedDecl *findSurvivor(NamedDecl *D) const {
206 for (Decl *Prev = D->getPreviousDecl(); Prev;
207 Prev = Prev->getPreviousDecl())
208 if (Prev->getTranslationUnitDecl() != DiscardedTU)
209 return dyn_cast<NamedDecl>(Prev);
210 return nullptr;
211 }
212
213 template <typename DeclT>
214 void unlinkRedeclChain(ASTContext &C, Redeclarable<DeclT> *DBase,
215 NamedDecl *PrevND) {
216 auto *Latest = static_cast<DeclT *>(DBase);
217 auto *Survivor = cast<DeclT>(PrevND);
218
219 // Rebuild First -> ... -> Survivor -> ... -> Latest as
220 // First -> ... -> Survivor.
221 Latest->getFirstDecl()->RedeclLink.setLatest(Survivor);
222
223 // The chain is circular: a withdrawn declaration still linked into it can
224 // never walk back around to itself, so redecls() on one would not
225 // terminate. Give each withdrawn declaration a chain of its own.
226 for (DeclT *Dead = Latest; Dead != Survivor;) {
227 DeclT *Next = Dead->getPreviousDecl();
228 Dead->First = Dead;
229 Dead->RedeclLink = Redeclarable<DeclT>::LatestDeclLink(C);
230 Dead = Next;
231 }
232 }
233
234 /// Remove entry from "C"'s lookup tables
235 void removeFromLookups(NamedDecl *D) {
236 if (D->getDeclName().isEmpty())
237 return;
238
239 if (D->getDeclName().isIdentifier() && D->getDeclName().getFETokenInfo() &&
240 !D->getLangOpts().ObjC && !D->getLangOpts().CPlusPlus)
241 S.IdResolver.RemoveDecl(D);
242
244 if (StoredDeclsMap *Map = ECCD->getPrimaryContext()->getLookupPtr()) {
245 auto It = Map->find(D->getDeclName());
246 if (It != Map->end())
247 It->second.remove(D);
248 }
249 }
250
251 /// Remove Decls defined in this DC from the lookup table
252 /// and restore the redeclaration chain to previous state
253 void VisitDeclContext(DeclContext *DC) {
254 llvm::SmallVector<Decl *, 8> Members(DC->decls());
255 for (Decl *M : Members) {
256 Visit(M); // restore redecls and lookup
257 // VisitNamedDecl() removes the named decls.
258 if (!isa<NamedDecl>(M))
259 DC->removeDecl(M);
260 }
261 }
262
263public:
265 : S(S), DiscardedTU(DiscardedTU) {}
266
267 void VisitDecl(Decl *D) {
268 if (auto *DC = dyn_cast<DeclContext>(D))
269 VisitDeclContext(DC);
270 }
271
272 /// Remove ND from its context and the lookup tables, and make the
273 /// declaration it redeclares visible again. Called by VisitRedeclarable()
274 /// before it restores the redeclaration chain, which hides that declaration.
276 // The templated decl of a template is not a member of its context: the
277 // template is.
279 if (!DC->containsDecl(ND))
280 return;
281 NamedDecl *Prev = findSurvivor(ND);
282 DC->removeDecl(ND);
283 removeFromLookups(ND);
284 // Like removeDecl(), use the semantic context: the out-of-line definition
285 // of a member is in DC, but it was removed from the lookup table of its
286 // class or namespace.
287 if (Prev)
289 }
290
291 /// Remove D like VisitNamedDecl() and restore its redeclaration chain to the
292 /// previous state. Returns the surviving previous declaration, if any.
293 /// DeclVisitor doesn't dispatch to it: Redeclarable is not a Decl class.
294 template <typename DeclT>
296 auto *D = static_cast<DeclT *>(DBase);
298 NamedDecl *Prev = findSurvivor(D);
299 if (Prev)
300 unlinkRedeclChain(S.getASTContext(), DBase, Prev);
301 return Prev;
302 }
303
309
311 // Some members of a definition are visible outside of it: the enumerators
312 // of an unscoped enumeration, and in C the tags and enumerators declared
313 // in a structure or union.
315 VisitDeclContext(D);
316
317 NamedDecl *Prev = VisitRedeclarable(D);
318
319 // A class definition is kept in DefinitionData outside the
320 // redeclaration chain
321 auto *RD = dyn_cast_if_present<CXXRecordDecl>(Prev);
322 if (!RD)
323 return;
324 if (CXXRecordDecl *Def = RD->getDefinition();
325 Def && Def->getTranslationUnitDecl() == DiscardedTU)
326 for (auto *R : RD->redecls())
327 cast<CXXRecordDecl>(R)->DefinitionData = nullptr;
328 }
329
334
336 // Handle cases of nested redeclarations like:
337 // PTU1: namespace outer { namespace ns { class Foo; } }
338 // PTU2: namespace outer { namespace ns { class Foo { ... }; error; } }
339 // Foo's redeclaration needs to be restored
340 VisitDeclContext(D);
342 }
343
344 void VisitTranslationUnitDecl(TranslationUnitDecl *D) { VisitDeclContext(D); }
345};
346
348 ASTDeclUnmerger(S, MostRecentTU).Visit(MostRecentTU);
349
350 // Lookup alone is not enough: the redeclaration chain still reaches these.
351 withdrawMostRecentTU(MostRecentTU);
352}
353
356 std::unique_ptr<llvm::Module> M /*={}*/) {
357 PTUs.emplace_back(PartialTranslationUnit());
358 PartialTranslationUnit &LastPTU = PTUs.back();
359 LastPTU.TUPart = TU;
360
361 if (!M)
362 M = Act->GenModule();
363
364 assert((!Act->getCodeGen() || M) && "Must have a llvm::Module at this point");
365
366 LastPTU.TheModule = std::move(M);
367 LLVM_DEBUG(llvm::dbgs() << "compile-ptu " << PTUs.size() - 1
368 << ": [TU=" << LastPTU.TUPart);
369 if (LastPTU.TheModule)
370 LLVM_DEBUG(llvm::dbgs() << ", M=" << LastPTU.TheModule.get() << " ("
371 << LastPTU.TheModule->getName() << ")");
372 LLVM_DEBUG(llvm::dbgs() << "]\n");
373 return LastPTU;
374}
375} // end namespace clang
Defines the clang::ASTContext interface.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the C++ template declaration subclasses.
Token Tok
The Token.
FormatToken * Next
The next token in the unwrapped line.
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
ExternCContextDecl * getExternCContextDecl() const
Removes decls introduced in the discarding PTU and restores the redeclaration chain to previous state...
void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D)
void VisitNamedDecl(NamedDecl *ND)
Remove ND from its context and the lookup tables, and make the declaration it redeclares visible agai...
void VisitFunctionDecl(FunctionDecl *D)
ASTDeclUnmerger(Sema &S, TranslationUnitDecl *DiscardedTU)
NamedDecl * VisitRedeclarable(Redeclarable< DeclT > *DBase)
Remove D like VisitNamedDecl() and restore its redeclaration chain to the previous state.
void VisitTypedefNameDecl(TypedefNameDecl *D)
void VisitTranslationUnitDecl(TranslationUnitDecl *D)
void VisitNamespaceDecl(NamespaceDecl *D)
void VisitUsingShadowDecl(UsingShadowDecl *D)
void VisitNamespaceAliasDecl(NamespaceAliasDecl *D)
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
CXXRecordDecl * getDefinition() const
Definition DeclCXX.h:549
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1467
void makeDeclVisibleInContext(NamedDecl *D)
Makes a declaration visible within this context.
void removeDecl(Decl *D)
Removes a declaration from this context.
bool containsDecl(Decl *D) const
Checks whether a declaration is in this context.
DeclContext * getPrimaryContext()
getPrimaryContext - There may be many different declarations of the same entity (including forward de...
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition DeclBase.h:2403
StoredDeclsMap * getLookupPtr() const
Retrieve the internal representation of the lookup structure.
Definition DeclBase.h:2701
A simple visitor class that helps create declaration visitors.
Definition DeclVisitor.h:68
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
Decl * getPreviousDecl()
Retrieve the previous declaration that declares the same entity as this declaration,...
Definition DeclBase.h:1079
ASTContext & getASTContext() const LLVM_READONLY
Definition DeclBase.cpp:550
DeclContext * getDeclContext()
Definition DeclBase.h:457
TranslationUnitDecl * getTranslationUnitDecl()
Definition DeclBase.cpp:535
DeclContext * getLexicalDeclContext()
getLexicalDeclContext - The declaration context where this Decl was lexically declared (LexicalDC).
Definition DeclBase.h:936
const LangOptions & getLangOpts() const LLVM_READONLY
Helper to get the language options from the ASTContext.
Definition DeclBase.cpp:556
void * getFETokenInfo() const
Get and set FETokenInfo.
bool isEmpty() const
Evaluates true when this declaration name is empty.
bool isIdentifier() const
Predicate functions for querying what type of name this is.
Declaration context for names declared as extern "C" in C++.
Definition Decl.h:249
Abstract interface for external sources of AST nodes.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
FileEntryRef getVirtualFileRef(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
Represents a function declaration or definition.
Definition Decl.h:2082
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
HeaderFileInfo & getFileInfo(FileEntryRef FE)
Return the HeaderFileInfo structure for the specified FileEntry, in preparation for updating it in so...
A custom action enabling the incremental processing functionality.
IncrementalParser(CompilerInstance &Instance, IncrementalAction *Act, llvm::Error &Err, std::list< PartialTranslationUnit > &PTUs)
IncrementalAction * Act
The FrontendAction used during incremental parsing.
std::list< PartialTranslationUnit > & PTUs
unsigned InputCount
Counts the number of direct user input lines that have been parsed.
void CleanUpPTU(TranslationUnitDecl *MostRecentTU)
PartialTranslationUnit & RegisterPTU(TranslationUnitDecl *TU, std::unique_ptr< llvm::Module > M={})
Register a PTU produced by Parse.
virtual llvm::Expected< TranslationUnitDecl * > Parse(llvm::StringRef Input)
Parses incremental input by creating an in-memory file.
std::unique_ptr< Parser > P
Parser.
ASTConsumer * Consumer
Consumer to process the produced top level decls. Owned by Act.
Sema & S
The Sema performing the incremental compilation.
This represents a decl that may have a name.
Definition Decl.h:276
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:342
Represents a C++ namespace alias.
Definition DeclCXX.h:3248
Represent a C++ namespace.
Definition Decl.h:594
Parser - This implements a parser for the C family of languages.
Definition Parser.h:264
OpaquePtr< DeclGroupRef > DeclGroupPtrTy
Definition Parser.h:313
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
bool isIncrementalProcessingEnabled() const
Returns true if incremental processing is enabled.
void Lex(Token &Result)
Lex the next token for this preprocessor.
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
HeaderSearch & getHeaderSearchInfo() const
const LangOptions & getLangOpts() const
Declaration of a redeclarable template.
Provides common interface for the Decls that can be redeclared.
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
DeclLink RedeclLink
Points to the next redeclaration in the chain.
static DeclLink LatestDeclLink(const ASTContext &Ctx)
@ DeclScope
This is a scope that can contain a declaration.
Definition Scope.h:63
Sema - This implements semantic analysis and AST building for C.
Definition Sema.h:863
void ActOnTranslationUnitScope(Scope *S)
Scope actions.
Definition Sema.cpp:173
ASTContext & getASTContext() const
Definition Sema.h:935
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition Sema.h:1491
ModuleImportState
An enumeration to represent the transition of states in parsing module fragments and imports.
Definition Sema.h:10124
Encodes a location in the source.
This class handles loading and caching of source files into memory.
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
FileManager & getFileManager() const
FileID getMainFileID() const
Returns the FileID of the main source file.
void overrideFileContents(FileEntryRef SourceFile, const llvm::MemoryBufferRef &Buffer)
Override the contents of the given source file by providing an already-allocated buffer.
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file.
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
bool isThisDeclarationADefinition() const
Return true if this declaration is a completion definition of the type.
Definition Decl.h:3994
NamedDecl * getTemplatedDecl() const
Get the underlying, templated declaration.
Token - This structure provides full information about a lexed token.
Definition Token.h:36
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition Token.h:104
The top declaration context.
Definition Decl.h:107
TranslationUnitDecl * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
TranslationUnitDecl * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3743
Represents a shadow declaration implicitly introduced into a scope by a (resolved) using-declaration ...
Definition DeclCXX.h:3446
Represents a variable declaration or definition.
Definition Decl.h:934
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ External
External linkage, which indicates that the entity can be referred to from other translation units.
Definition Linkage.h:58
U cast(CodeGen::Address addr)
Definition Address.h:327
The class keeps track of various objects created as part of processing incremental inputs.
std::unique_ptr< llvm::Module > TheModule
The llvm IR produced for the input.