clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SemaFixItUtils.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include/clang/Sema -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangSema/../include -I /usr/src/gnu/usr.bin/clang/libclangSema/obj -I /usr/src/gnu/usr.bin/clang/libclangSema/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangSema/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangSema/../../../llvm/clang/lib/Sema/SemaFixItUtils.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H |
15 | #define LLVM_CLANG_LEX_PREPROCESSOR_H |
16 | |
17 | #include "clang/Basic/Diagnostic.h" |
18 | #include "clang/Basic/IdentifierTable.h" |
19 | #include "clang/Basic/LLVM.h" |
20 | #include "clang/Basic/LangOptions.h" |
21 | #include "clang/Basic/Module.h" |
22 | #include "clang/Basic/SourceLocation.h" |
23 | #include "clang/Basic/SourceManager.h" |
24 | #include "clang/Basic/TokenKinds.h" |
25 | #include "clang/Lex/Lexer.h" |
26 | #include "clang/Lex/MacroInfo.h" |
27 | #include "clang/Lex/ModuleLoader.h" |
28 | #include "clang/Lex/ModuleMap.h" |
29 | #include "clang/Lex/PPCallbacks.h" |
30 | #include "clang/Lex/PreprocessorExcludedConditionalDirectiveSkipMapping.h" |
31 | #include "clang/Lex/Token.h" |
32 | #include "clang/Lex/TokenLexer.h" |
33 | #include "llvm/ADT/ArrayRef.h" |
34 | #include "llvm/ADT/DenseMap.h" |
35 | #include "llvm/ADT/FoldingSet.h" |
36 | #include "llvm/ADT/FunctionExtras.h" |
37 | #include "llvm/ADT/None.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerUnion.h" |
40 | #include "llvm/ADT/STLExtras.h" |
41 | #include "llvm/ADT/SmallPtrSet.h" |
42 | #include "llvm/ADT/SmallVector.h" |
43 | #include "llvm/ADT/StringRef.h" |
44 | #include "llvm/ADT/TinyPtrVector.h" |
45 | #include "llvm/ADT/iterator_range.h" |
46 | #include "llvm/Support/Allocator.h" |
47 | #include "llvm/Support/Casting.h" |
48 | #include "llvm/Support/Registry.h" |
49 | #include <cassert> |
50 | #include <cstddef> |
51 | #include <cstdint> |
52 | #include <map> |
53 | #include <memory> |
54 | #include <string> |
55 | #include <utility> |
56 | #include <vector> |
57 | |
58 | namespace llvm { |
59 | |
60 | template<unsigned InternalLen> class SmallString; |
61 | |
62 | } |
63 | |
64 | namespace clang { |
65 | |
66 | class CodeCompletionHandler; |
67 | class CommentHandler; |
68 | class DirectoryEntry; |
69 | class DirectoryLookup; |
70 | class EmptylineHandler; |
71 | class ExternalPreprocessorSource; |
72 | class FileEntry; |
73 | class FileManager; |
74 | class HeaderSearch; |
75 | class MacroArgs; |
76 | class PragmaHandler; |
77 | class PragmaNamespace; |
78 | class PreprocessingRecord; |
79 | class PreprocessorLexer; |
80 | class PreprocessorOptions; |
81 | class ScratchBuffer; |
82 | class TargetInfo; |
83 | |
84 | namespace Builtin { |
85 | class Context; |
86 | } |
87 | |
88 | |
89 | |
90 | class TokenValue { |
91 | tok::TokenKind Kind; |
92 | IdentifierInfo *II; |
93 | |
94 | public: |
95 | TokenValue(tok::TokenKind Kind) : Kind(Kind), II(nullptr) { |
96 | assert(Kind != tok::raw_identifier && "Raw identifiers are not supported."); |
97 | assert(Kind != tok::identifier && |
98 | "Identifiers should be created by TokenValue(IdentifierInfo *)"); |
99 | assert(!tok::isLiteral(Kind) && "Literals are not supported."); |
100 | assert(!tok::isAnnotation(Kind) && "Annotations are not supported."); |
101 | } |
102 | |
103 | TokenValue(IdentifierInfo *II) : Kind(tok::identifier), II(II) {} |
104 | |
105 | bool operator==(const Token &Tok) const { |
106 | return Tok.getKind() == Kind && |
107 | (!II || II == Tok.getIdentifierInfo()); |
108 | } |
109 | }; |
110 | |
111 | |
112 | enum MacroUse { |
113 | |
114 | MU_Other = 0, |
115 | |
116 | |
117 | MU_Define = 1, |
118 | |
119 | |
120 | MU_Undef = 2 |
121 | }; |
122 | |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | class Preprocessor { |
130 | friend class VAOptDefinitionContext; |
131 | friend class VariadicMacroScopeGuard; |
132 | |
133 | llvm::unique_function<void(const clang::Token &)> OnToken; |
134 | std::shared_ptr<PreprocessorOptions> PPOpts; |
135 | DiagnosticsEngine *Diags; |
136 | LangOptions &LangOpts; |
137 | const TargetInfo *Target = nullptr; |
138 | const TargetInfo *AuxTarget = nullptr; |
139 | FileManager &FileMgr; |
140 | SourceManager &SourceMgr; |
141 | std::unique_ptr<ScratchBuffer> ScratchBuf; |
142 | HeaderSearch &HeaderInfo; |
143 | ModuleLoader &TheModuleLoader; |
144 | |
145 | |
146 | ExternalPreprocessorSource *ExternalSource; |
147 | |
148 | |
149 | |
150 | llvm::BumpPtrAllocator BP; |
151 | |
152 | |
153 | IdentifierInfo *Ident__LINE__, *Ident__FILE__; |
154 | IdentifierInfo *Ident__DATE__, *Ident__TIME__; |
155 | IdentifierInfo *Ident__INCLUDE_LEVEL__; |
156 | IdentifierInfo *Ident__BASE_FILE__; |
157 | IdentifierInfo *Ident__FILE_NAME__; |
158 | IdentifierInfo *Ident__TIMESTAMP__; |
159 | IdentifierInfo *Ident__COUNTER__; |
160 | IdentifierInfo *Ident_Pragma, *Ident__pragma; |
161 | IdentifierInfo *Ident__identifier; |
162 | IdentifierInfo *Ident__VA_ARGS__; |
163 | IdentifierInfo *Ident__VA_OPT__; |
164 | IdentifierInfo *Ident__has_feature; |
165 | IdentifierInfo *Ident__has_extension; |
166 | IdentifierInfo *Ident__has_builtin; |
167 | IdentifierInfo *Ident__has_attribute; |
168 | IdentifierInfo *Ident__has_include; |
169 | IdentifierInfo *Ident__has_include_next; |
170 | IdentifierInfo *Ident__has_warning; |
171 | IdentifierInfo *Ident__is_identifier; |
172 | IdentifierInfo *Ident__building_module; |
173 | IdentifierInfo *Ident__MODULE__; |
174 | IdentifierInfo *Ident__has_cpp_attribute; |
175 | IdentifierInfo *Ident__has_c_attribute; |
176 | IdentifierInfo *Ident__has_declspec; |
177 | IdentifierInfo *Ident__is_target_arch; |
178 | IdentifierInfo *Ident__is_target_vendor; |
179 | IdentifierInfo *Ident__is_target_os; |
180 | IdentifierInfo *Ident__is_target_environment; |
181 | |
182 | |
183 | Token* ArgMacro; |
184 | |
185 | SourceLocation DATELoc, TIMELoc; |
186 | |
187 | |
188 | unsigned CounterValue = 0; |
189 | |
190 | enum { |
191 | |
192 | MaxAllowedIncludeStackDepth = 200 |
193 | }; |
194 | |
195 | |
196 | bool KeepComments : 1; |
197 | bool KeepMacroComments : 1; |
198 | bool SuppressIncludeNotFoundError : 1; |
199 | |
200 | |
201 | bool InMacroArgs : 1; |
202 | |
203 | |
204 | bool OwnsHeaderSearch : 1; |
205 | |
206 | |
207 | bool DisableMacroExpansion : 1; |
208 | |
209 | |
210 | |
211 | bool MacroExpansionInDirectivesOverride : 1; |
212 | |
213 | class ResetMacroExpansionHelper; |
214 | |
215 | |
216 | mutable bool ReadMacrosFromExternalSource : 1; |
217 | |
218 | |
219 | bool PragmasEnabled : 1; |
220 | |
221 | |
222 | bool PreprocessedOutput : 1; |
223 | |
224 | |
225 | bool ParsingIfOrElifDirective; |
226 | |
227 | |
228 | bool InMacroArgPreExpansion; |
229 | |
230 | |
231 | |
232 | mutable IdentifierTable Identifiers; |
233 | |
234 | |
235 | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 | |
243 | SelectorTable Selectors; |
244 | |
245 | |
246 | std::unique_ptr<Builtin::Context> BuiltinInfo; |
247 | |
248 | |
249 | |
250 | std::unique_ptr<PragmaNamespace> PragmaHandlers; |
251 | |
252 | |
253 | |
254 | std::unique_ptr<PragmaNamespace> PragmaHandlersBackup; |
255 | |
256 | |
257 | |
258 | std::vector<CommentHandler *> CommentHandlers; |
259 | |
260 | |
261 | EmptylineHandler *Emptyline = nullptr; |
262 | |
263 | |
264 | |
265 | bool IncrementalProcessing = false; |
266 | |
267 | public: |
268 | |
269 | const TranslationUnitKind TUKind; |
270 | |
271 | private: |
272 | |
273 | CodeCompletionHandler *CodeComplete = nullptr; |
274 | |
275 | |
276 | const FileEntry *CodeCompletionFile = nullptr; |
277 | |
278 | |
279 | unsigned CodeCompletionOffset = 0; |
280 | |
281 | |
282 | |
283 | SourceLocation CodeCompletionLoc; |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | SourceLocation CodeCompletionFileLoc; |
290 | |
291 | |
292 | |
293 | SourceLocation ModuleImportLoc; |
294 | |
295 | |
296 | SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> ModuleImportPath; |
297 | |
298 | |
299 | bool LastTokenWasAt = false; |
300 | |
301 | |
302 | class ImportSeq { |
303 | public: |
304 | enum State : int { |
305 | |
306 | AtTopLevel = 0, |
307 | AfterTopLevelTokenSeq = -1, |
308 | AfterExport = -2, |
309 | AfterImportSeq = -3, |
310 | }; |
311 | |
312 | ImportSeq(State S) : S(S) {} |
313 | |
314 | |
315 | void handleOpenBracket() { |
316 | S = static_cast<State>(std::max<int>(S, 0) + 1); |
317 | } |
318 | |
319 | void handleCloseBracket() { |
320 | S = static_cast<State>(std::max<int>(S, 1) - 1); |
321 | } |
322 | |
323 | void handleCloseBrace() { |
324 | handleCloseBracket(); |
325 | if (S == AtTopLevel && !AfterHeaderName) |
326 | S = AfterTopLevelTokenSeq; |
327 | } |
328 | |
329 | void handleSemi() { |
330 | if (atTopLevel()) { |
331 | S = AfterTopLevelTokenSeq; |
332 | AfterHeaderName = false; |
333 | } |
334 | } |
335 | |
336 | |
337 | void handleExport() { |
338 | if (S == AfterTopLevelTokenSeq) |
339 | S = AfterExport; |
340 | else if (S <= 0) |
341 | S = AtTopLevel; |
342 | } |
343 | |
344 | void handleImport() { |
345 | if (S == AfterTopLevelTokenSeq || S == AfterExport) |
346 | S = AfterImportSeq; |
347 | else if (S <= 0) |
348 | S = AtTopLevel; |
349 | } |
350 | |
351 | |
352 | |
353 | void handleHeaderName() { |
354 | if (S == AfterImportSeq) |
355 | AfterHeaderName = true; |
356 | handleMisc(); |
357 | } |
358 | |
359 | |
360 | void handleMisc() { |
361 | if (S <= 0) |
362 | S = AtTopLevel; |
363 | } |
364 | |
365 | bool atTopLevel() { return S <= 0; } |
366 | bool afterImportSeq() { return S == AfterImportSeq; } |
367 | |
368 | private: |
369 | State S; |
370 | |
371 | |
372 | |
373 | bool AfterHeaderName = false; |
374 | }; |
375 | |
376 | |
377 | ImportSeq ImportSeqState = ImportSeq::AfterTopLevelTokenSeq; |
378 | |
379 | |
380 | |
381 | bool ModuleImportExpectsIdentifier = false; |
382 | |
383 | |
384 | |
385 | std::pair<IdentifierInfo *, SourceLocation> PragmaARCCFCodeAuditedInfo; |
386 | |
387 | |
388 | |
389 | SourceLocation PragmaAssumeNonNullLoc; |
390 | |
391 | |
392 | bool CodeCompletionReached = false; |
393 | |
394 | |
395 | |
396 | IdentifierInfo *CodeCompletionII = nullptr; |
397 | |
398 | |
399 | SourceRange CodeCompletionTokenRange; |
400 | |
401 | |
402 | |
403 | |
404 | const DirectoryEntry *MainFileDir = nullptr; |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | std::pair<int, bool> SkipMainFilePreamble; |
412 | |
413 | |
414 | |
415 | bool HasReachedMaxIncludeDepth = false; |
416 | |
417 | |
418 | |
419 | |
420 | |
421 | |
422 | |
423 | unsigned LexLevel = 0; |
424 | |
425 | |
426 | unsigned TokenCount = 0; |
427 | |
428 | |
429 | bool PreprocessToken = false; |
430 | |
431 | |
432 | |
433 | unsigned MaxTokens = 0; |
434 | SourceLocation MaxTokensOverrideLoc; |
435 | |
436 | public: |
437 | struct PreambleSkipInfo { |
438 | SourceLocation HashTokenLoc; |
439 | SourceLocation IfTokenLoc; |
440 | bool FoundNonSkipPortion; |
441 | bool FoundElse; |
442 | SourceLocation ElseLoc; |
443 | |
444 | PreambleSkipInfo(SourceLocation HashTokenLoc, SourceLocation IfTokenLoc, |
445 | bool FoundNonSkipPortion, bool FoundElse, |
446 | SourceLocation ElseLoc) |
447 | : HashTokenLoc(HashTokenLoc), IfTokenLoc(IfTokenLoc), |
448 | FoundNonSkipPortion(FoundNonSkipPortion), FoundElse(FoundElse), |
449 | ElseLoc(ElseLoc) {} |
450 | }; |
451 | |
452 | private: |
453 | friend class ASTReader; |
454 | friend class MacroArgs; |
455 | |
456 | class PreambleConditionalStackStore { |
457 | enum State { |
458 | Off = 0, |
459 | Recording = 1, |
460 | Replaying = 2, |
461 | }; |
462 | |
463 | public: |
464 | PreambleConditionalStackStore() = default; |
465 | |
466 | void startRecording() { ConditionalStackState = Recording; } |
467 | void startReplaying() { ConditionalStackState = Replaying; } |
468 | bool isRecording() const { return ConditionalStackState == Recording; } |
469 | bool isReplaying() const { return ConditionalStackState == Replaying; } |
470 | |
471 | ArrayRef<PPConditionalInfo> getStack() const { |
472 | return ConditionalStack; |
473 | } |
474 | |
475 | void doneReplaying() { |
476 | ConditionalStack.clear(); |
477 | ConditionalStackState = Off; |
478 | } |
479 | |
480 | void setStack(ArrayRef<PPConditionalInfo> s) { |
481 | if (!isRecording() && !isReplaying()) |
482 | return; |
483 | ConditionalStack.clear(); |
484 | ConditionalStack.append(s.begin(), s.end()); |
485 | } |
486 | |
487 | bool hasRecordedPreamble() const { return !ConditionalStack.empty(); } |
488 | |
489 | bool reachedEOFWhileSkipping() const { return SkipInfo.hasValue(); } |
490 | |
491 | void clearSkipInfo() { SkipInfo.reset(); } |
492 | |
493 | llvm::Optional<PreambleSkipInfo> SkipInfo; |
494 | |
495 | private: |
496 | SmallVector<PPConditionalInfo, 4> ConditionalStack; |
497 | State ConditionalStackState = Off; |
498 | } PreambleConditionalStack; |
499 | |
500 | |
501 | |
502 | |
503 | |
504 | std::unique_ptr<Lexer> CurLexer; |
505 | |
506 | |
507 | |
508 | |
509 | |
510 | PreprocessorLexer *CurPPLexer = nullptr; |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | const DirectoryLookup *CurDirLookup = nullptr; |
518 | |
519 | |
520 | |
521 | |
522 | std::unique_ptr<TokenLexer> CurTokenLexer; |
523 | |
524 | |
525 | enum CurLexerKind { |
526 | CLK_Lexer, |
527 | CLK_TokenLexer, |
528 | CLK_CachingLexer, |
529 | CLK_LexAfterModuleImport |
530 | } CurLexerKind = CLK_Lexer; |
531 | |
532 | |
533 | |
534 | Module *CurLexerSubmodule = nullptr; |
535 | |
536 | |
537 | |
538 | |
539 | struct IncludeStackInfo { |
540 | enum CurLexerKind CurLexerKind; |
541 | Module *TheSubmodule; |
542 | std::unique_ptr<Lexer> TheLexer; |
543 | PreprocessorLexer *ThePPLexer; |
544 | std::unique_ptr<TokenLexer> TheTokenLexer; |
545 | const DirectoryLookup *TheDirLookup; |
546 | |
547 | |
548 | |
549 | IncludeStackInfo(enum CurLexerKind CurLexerKind, Module *TheSubmodule, |
550 | std::unique_ptr<Lexer> &&TheLexer, |
551 | PreprocessorLexer *ThePPLexer, |
552 | std::unique_ptr<TokenLexer> &&TheTokenLexer, |
553 | const DirectoryLookup *TheDirLookup) |
554 | : CurLexerKind(std::move(CurLexerKind)), |
555 | TheSubmodule(std::move(TheSubmodule)), TheLexer(std::move(TheLexer)), |
556 | ThePPLexer(std::move(ThePPLexer)), |
557 | TheTokenLexer(std::move(TheTokenLexer)), |
558 | TheDirLookup(std::move(TheDirLookup)) {} |
559 | }; |
560 | std::vector<IncludeStackInfo> IncludeMacroStack; |
561 | |
562 | |
563 | |
564 | std::unique_ptr<PPCallbacks> Callbacks; |
565 | |
566 | struct MacroExpandsInfo { |
567 | Token Tok; |
568 | MacroDefinition MD; |
569 | SourceRange Range; |
570 | |
571 | MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range) |
572 | : Tok(Tok), MD(MD), Range(Range) {} |
573 | }; |
574 | SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks; |
575 | |
576 | |
577 | struct ModuleMacroInfo { |
578 | |
579 | MacroDirective *MD; |
580 | |
581 | |
582 | llvm::TinyPtrVector<ModuleMacro *> ActiveModuleMacros; |
583 | |
584 | |
585 | |
586 | unsigned ActiveModuleMacrosGeneration = 0; |
587 | |
588 | |
589 | bool IsAmbiguous = false; |
590 | |
591 | |
592 | llvm::TinyPtrVector<ModuleMacro *> OverriddenMacros; |
593 | |
594 | ModuleMacroInfo(MacroDirective *MD) : MD(MD) {} |
595 | }; |
596 | |
597 | |
598 | class MacroState { |
599 | mutable llvm::PointerUnion<MacroDirective *, ModuleMacroInfo *> State; |
600 | |
601 | ModuleMacroInfo *getModuleInfo(Preprocessor &PP, |
602 | const IdentifierInfo *II) const { |
603 | if (II->isOutOfDate()) |
| 11 | | Assuming the condition is false | |
|
| |
604 | PP.updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II)); |
605 | |
606 | |
607 | if (!II->hasMacroDefinition() || |
| 13 | | Assuming the condition is false | |
|
| |
608 | (!PP.getLangOpts().Modules && |
| 14 | | Assuming field 'Modules' is not equal to 0 | |
|
609 | !PP.getLangOpts().ModulesLocalVisibility) || |
610 | !PP.CurSubmoduleState->VisibleModules.getGeneration()) |
| 15 | | Assuming the condition is false | |
|
611 | return nullptr; |
612 | |
613 | auto *Info = State.dyn_cast<ModuleMacroInfo*>(); |
614 | if (!Info) { |
| |
615 | Info = new (PP.getPreprocessorAllocator()) |
| 18 | | Calling 'operator new<llvm::MallocAllocator, 4096UL, 4096UL, 128UL>' | |
|
616 | ModuleMacroInfo(State.get<MacroDirective *>()); |
617 | State = Info; |
618 | } |
619 | |
620 | if (PP.CurSubmoduleState->VisibleModules.getGeneration() != |
621 | Info->ActiveModuleMacrosGeneration) |
622 | PP.updateModuleMacroInfo(II, *Info); |
623 | return Info; |
624 | } |
625 | |
626 | public: |
627 | MacroState() : MacroState(nullptr) {} |
628 | MacroState(MacroDirective *MD) : State(MD) {} |
629 | |
630 | MacroState(MacroState &&O) noexcept : State(O.State) { |
631 | O.State = (MacroDirective *)nullptr; |
632 | } |
633 | |
634 | MacroState &operator=(MacroState &&O) noexcept { |
635 | auto S = O.State; |
636 | O.State = (MacroDirective *)nullptr; |
637 | State = S; |
638 | return *this; |
639 | } |
640 | |
641 | ~MacroState() { |
642 | if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) |
643 | Info->~ModuleMacroInfo(); |
644 | } |
645 | |
646 | MacroDirective *getLatest() const { |
647 | if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) |
648 | return Info->MD; |
649 | return State.get<MacroDirective*>(); |
650 | } |
651 | |
652 | void setLatest(MacroDirective *MD) { |
653 | if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) |
654 | Info->MD = MD; |
655 | else |
656 | State = MD; |
657 | } |
658 | |
659 | bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const { |
660 | auto *Info = getModuleInfo(PP, II); |
661 | return Info ? Info->IsAmbiguous : false; |
662 | } |
663 | |
664 | ArrayRef<ModuleMacro *> |
665 | getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const { |
666 | if (auto *Info = getModuleInfo(PP, II)) |
| 10 | | Calling 'MacroState::getModuleInfo' | |
|
667 | return Info->ActiveModuleMacros; |
668 | return None; |
669 | } |
670 | |
671 | MacroDirective::DefInfo findDirectiveAtLoc(SourceLocation Loc, |
672 | SourceManager &SourceMgr) const { |
673 | |
674 | if (auto *Latest = getLatest()) |
675 | return Latest->findDirectiveAtLoc(Loc, SourceMgr); |
676 | return {}; |
677 | } |
678 | |
679 | void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) { |
680 | if (auto *Info = getModuleInfo(PP, II)) { |
681 | Info->OverriddenMacros.insert(Info->OverriddenMacros.end(), |
682 | Info->ActiveModuleMacros.begin(), |
683 | Info->ActiveModuleMacros.end()); |
684 | Info->ActiveModuleMacros.clear(); |
685 | Info->IsAmbiguous = false; |
686 | } |
687 | } |
688 | |
689 | ArrayRef<ModuleMacro*> getOverriddenMacros() const { |
690 | if (auto *Info = State.dyn_cast<ModuleMacroInfo*>()) |
691 | return Info->OverriddenMacros; |
692 | return None; |
693 | } |
694 | |
695 | void setOverriddenMacros(Preprocessor &PP, |
696 | ArrayRef<ModuleMacro *> Overrides) { |
697 | auto *Info = State.dyn_cast<ModuleMacroInfo*>(); |
698 | if (!Info) { |
699 | if (Overrides.empty()) |
700 | return; |
701 | Info = new (PP.getPreprocessorAllocator()) |
702 | ModuleMacroInfo(State.get<MacroDirective *>()); |
703 | State = Info; |
704 | } |
705 | Info->OverriddenMacros.clear(); |
706 | Info->OverriddenMacros.insert(Info->OverriddenMacros.end(), |
707 | Overrides.begin(), Overrides.end()); |
708 | Info->ActiveModuleMacrosGeneration = 0; |
709 | } |
710 | }; |
711 | |
712 | |
713 | |
714 | |
715 | |
716 | |
717 | using MacroMap = llvm::DenseMap<const IdentifierInfo *, MacroState>; |
718 | |
719 | struct SubmoduleState; |
720 | |
721 | |
722 | struct BuildingSubmoduleInfo { |
723 | |
724 | Module *M; |
725 | |
726 | |
727 | SourceLocation ImportLoc; |
728 | |
729 | |
730 | bool IsPragma; |
731 | |
732 | |
733 | SubmoduleState *OuterSubmoduleState; |
734 | |
735 | |
736 | unsigned OuterPendingModuleMacroNames; |
737 | |
738 | BuildingSubmoduleInfo(Module *M, SourceLocation ImportLoc, bool IsPragma, |
739 | SubmoduleState *OuterSubmoduleState, |
740 | unsigned OuterPendingModuleMacroNames) |
741 | : M(M), ImportLoc(ImportLoc), IsPragma(IsPragma), |
742 | OuterSubmoduleState(OuterSubmoduleState), |
743 | OuterPendingModuleMacroNames(OuterPendingModuleMacroNames) {} |
744 | }; |
745 | SmallVector<BuildingSubmoduleInfo, 8> BuildingSubmoduleStack; |
746 | |
747 | |
748 | struct SubmoduleState { |
749 | |
750 | MacroMap Macros; |
751 | |
752 | |
753 | VisibleModuleSet VisibleModules; |
754 | |
755 | |
756 | |
757 | }; |
758 | std::map<Module *, SubmoduleState> Submodules; |
759 | |
760 | |
761 | SubmoduleState NullSubmoduleState; |
762 | |
763 | |
764 | |
765 | SubmoduleState *CurSubmoduleState; |
766 | |
767 | |
768 | llvm::FoldingSet<ModuleMacro> ModuleMacros; |
769 | |
770 | |
771 | llvm::SmallVector<const IdentifierInfo *, 32> PendingModuleMacroNames; |
772 | |
773 | |
774 | |
775 | llvm::DenseMap<const IdentifierInfo *, llvm::TinyPtrVector<ModuleMacro *>> |
776 | LeafModuleMacros; |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | using WarnUnusedMacroLocsTy = llvm::SmallDenseSet<SourceLocation, 32>; |
787 | WarnUnusedMacroLocsTy WarnUnusedMacroLocs; |
788 | |
789 | |
790 | |
791 | MacroArgs *MacroArgCache = nullptr; |
792 | |
793 | |
794 | |
795 | llvm::DenseMap<IdentifierInfo *, std::vector<MacroInfo *>> |
796 | PragmaPushMacroInfo; |
797 | |
798 | |
799 | unsigned NumDirectives = 0; |
800 | unsigned NumDefined = 0; |
801 | unsigned NumUndefined = 0; |
802 | unsigned NumPragma = 0; |
803 | unsigned NumIf = 0; |
804 | unsigned NumElse = 0; |
805 | unsigned NumEndif = 0; |
806 | unsigned NumEnteredSourceFiles = 0; |
807 | unsigned MaxIncludeStackDepth = 0; |
808 | unsigned NumMacroExpanded = 0; |
809 | unsigned NumFnMacroExpanded = 0; |
810 | unsigned NumBuiltinMacroExpanded = 0; |
811 | unsigned NumFastMacroExpanded = 0; |
812 | unsigned NumTokenPaste = 0; |
813 | unsigned NumFastTokenPaste = 0; |
814 | unsigned NumSkipped = 0; |
815 | |
816 | |
817 | |
818 | std::string Predefines; |
819 | |
820 | |
821 | FileID PredefinesFileID; |
822 | |
823 | |
824 | FileID PCHThroughHeaderFileID; |
825 | |
826 | |
827 | bool SkippingUntilPragmaHdrStop = false; |
828 | |
829 | |
830 | bool SkippingUntilPCHThroughHeader = false; |
831 | |
832 | |
833 | |
834 | enum { TokenLexerCacheSize = 8 }; |
835 | unsigned NumCachedTokenLexers; |
836 | std::unique_ptr<TokenLexer> TokenLexerCache[TokenLexerCacheSize]; |
837 | |
838 | |
839 | |
840 | |
841 | |
842 | |
843 | |
844 | SmallVector<Token, 16> MacroExpandedTokens; |
845 | std::vector<std::pair<TokenLexer *, size_t>> MacroExpandingLexersStack; |
846 | |
847 | |
848 | |
849 | |
850 | |
851 | |
852 | PreprocessingRecord *Record = nullptr; |
853 | |
854 | |
855 | using CachedTokensTy = SmallVector<Token, 1>; |
856 | |
857 | |
858 | |
859 | CachedTokensTy CachedTokens; |
860 | |
861 | |
862 | |
863 | |
864 | |
865 | |
866 | CachedTokensTy::size_type CachedLexPos = 0; |
867 | |
868 | |
869 | |
870 | |
871 | |
872 | |
873 | std::vector<CachedTokensTy::size_type> BacktrackPositions; |
874 | |
875 | struct MacroInfoChain { |
876 | MacroInfo MI; |
877 | MacroInfoChain *Next; |
878 | }; |
879 | |
880 | |
881 | |
882 | MacroInfoChain *MIChainHead = nullptr; |
883 | |
884 | void updateOutOfDateIdentifier(IdentifierInfo &II) const; |
885 | |
886 | public: |
887 | Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts, |
888 | DiagnosticsEngine &diags, LangOptions &opts, SourceManager &SM, |
889 | HeaderSearch &Headers, ModuleLoader &TheModuleLoader, |
890 | IdentifierInfoLookup *IILookup = nullptr, |
891 | bool OwnsHeaderSearch = false, |
892 | TranslationUnitKind TUKind = TU_Complete); |
893 | |
894 | ~Preprocessor(); |
895 | |
896 | |
897 | |
898 | |
899 | |
900 | |
901 | |
902 | void Initialize(const TargetInfo &Target, |
903 | const TargetInfo *AuxTarget = nullptr); |
904 | |
905 | |
906 | |
907 | |
908 | |
909 | |
910 | |
911 | void InitializeForModelFile(); |
912 | |
913 | |
914 | void FinalizeForModelFile(); |
915 | |
916 | |
917 | |
918 | PreprocessorOptions &getPreprocessorOpts() const { return *PPOpts; } |
919 | |
920 | DiagnosticsEngine &getDiagnostics() const { return *Diags; } |
921 | void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } |
922 | |
923 | const LangOptions &getLangOpts() const { return LangOpts; } |
924 | const TargetInfo &getTargetInfo() const { return *Target; } |
925 | const TargetInfo *getAuxTargetInfo() const { return AuxTarget; } |
926 | FileManager &getFileManager() const { return FileMgr; } |
927 | SourceManager &getSourceManager() const { return SourceMgr; } |
928 | HeaderSearch &getHeaderSearchInfo() const { return HeaderInfo; } |
929 | |
930 | IdentifierTable &getIdentifierTable() { return Identifiers; } |
931 | const IdentifierTable &getIdentifierTable() const { return Identifiers; } |
932 | SelectorTable &getSelectorTable() { return Selectors; } |
933 | Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; } |
934 | llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; } |
935 | |
936 | void setExternalSource(ExternalPreprocessorSource *Source) { |
937 | ExternalSource = Source; |
938 | } |
939 | |
940 | ExternalPreprocessorSource *getExternalSource() const { |
941 | return ExternalSource; |
942 | } |
943 | |
944 | |
945 | ModuleLoader &getModuleLoader() const { return TheModuleLoader; } |
946 | |
947 | bool hadModuleLoaderFatalFailure() const { |
948 | return TheModuleLoader.HadFatalFailure; |
949 | } |
950 | |
951 | |
952 | |
953 | unsigned getNumDirectives() const { |
954 | return NumDirectives; |
955 | } |
956 | |
957 | |
958 | bool isParsingIfOrElifDirective() const { |
959 | return ParsingIfOrElifDirective; |
960 | } |
961 | |
962 | |
963 | void SetCommentRetentionState(bool KeepComments, bool KeepMacroComments) { |
964 | this->KeepComments = KeepComments | KeepMacroComments; |
965 | this->KeepMacroComments = KeepMacroComments; |
966 | } |
967 | |
968 | bool getCommentRetentionState() const { return KeepComments; } |
969 | |
970 | void setPragmasEnabled(bool Enabled) { PragmasEnabled = Enabled; } |
971 | bool getPragmasEnabled() const { return PragmasEnabled; } |
972 | |
973 | void SetSuppressIncludeNotFoundError(bool Suppress) { |
974 | SuppressIncludeNotFoundError = Suppress; |
975 | } |
976 | |
977 | bool GetSuppressIncludeNotFoundError() { |
978 | return SuppressIncludeNotFoundError; |
979 | } |
980 | |
981 | |
982 | |
983 | void setPreprocessedOutput(bool IsPreprocessedOutput) { |
984 | PreprocessedOutput = IsPreprocessedOutput; |
985 | } |
986 | |
987 | |
988 | |
989 | bool isPreprocessedOutput() const { return PreprocessedOutput; } |
990 | |
991 | |
992 | bool isCurrentLexer(const PreprocessorLexer *L) const { |
993 | return CurPPLexer == L; |
994 | } |
995 | |
996 | |
997 | |
998 | |
999 | |
1000 | PreprocessorLexer *getCurrentLexer() const { return CurPPLexer; } |
1001 | |
1002 | |
1003 | |
1004 | |
1005 | |
1006 | PreprocessorLexer *getCurrentFileLexer() const; |
1007 | |
1008 | |
1009 | |
1010 | Module *getCurrentLexerSubmodule() const { return CurLexerSubmodule; } |
1011 | |
1012 | |
1013 | FileID getPredefinesFileID() const { return PredefinesFileID; } |
1014 | |
1015 | |
1016 | |
1017 | |
1018 | |
1019 | |
1020 | PPCallbacks *getPPCallbacks() const { return Callbacks.get(); } |
1021 | void addPPCallbacks(std::unique_ptr<PPCallbacks> C) { |
1022 | if (Callbacks) |
1023 | C = std::make_unique<PPChainedCallbacks>(std::move(C), |
1024 | std::move(Callbacks)); |
1025 | Callbacks = std::move(C); |
1026 | } |
1027 | |
1028 | |
1029 | |
1030 | unsigned getTokenCount() const { return TokenCount; } |
1031 | |
1032 | |
1033 | unsigned getMaxTokens() const { return MaxTokens; } |
1034 | |
1035 | void overrideMaxTokens(unsigned Value, SourceLocation Loc) { |
1036 | MaxTokens = Value; |
1037 | MaxTokensOverrideLoc = Loc; |
1038 | }; |
1039 | |
1040 | SourceLocation getMaxTokensOverrideLoc() const { return MaxTokensOverrideLoc; } |
1041 | |
1042 | |
1043 | |
1044 | |
1045 | void setTokenWatcher(llvm::unique_function<void(const clang::Token &)> F) { |
1046 | OnToken = std::move(F); |
1047 | } |
1048 | |
1049 | void setPreprocessToken(bool Preprocess) { PreprocessToken = Preprocess; } |
1050 | |
1051 | bool isMacroDefined(StringRef Id) { |
1052 | return isMacroDefined(&Identifiers.get(Id)); |
1053 | } |
1054 | bool isMacroDefined(const IdentifierInfo *II) { |
1055 | return II->hasMacroDefinition() && |
1056 | (!getLangOpts().Modules || (bool)getMacroDefinition(II)); |
1057 | } |
1058 | |
1059 | |
1060 | |
1061 | |
1062 | bool isMacroDefinedInLocalModule(const IdentifierInfo *II, Module *M) { |
1063 | if (!II->hasMacroDefinition()) |
1064 | return false; |
1065 | auto I = Submodules.find(M); |
1066 | if (I == Submodules.end()) |
1067 | return false; |
1068 | auto J = I->second.Macros.find(II); |
1069 | if (J == I->second.Macros.end()) |
1070 | return false; |
1071 | auto *MD = J->second.getLatest(); |
1072 | return MD && MD->isDefined(); |
1073 | } |
1074 | |
1075 | MacroDefinition getMacroDefinition(const IdentifierInfo *II) { |
1076 | if (!II->hasMacroDefinition()) |
1077 | return {}; |
1078 | |
1079 | MacroState &S = CurSubmoduleState->Macros[II]; |
1080 | auto *MD = S.getLatest(); |
1081 | while (MD && isa<VisibilityMacroDirective>(MD)) |
1082 | MD = MD->getPrevious(); |
1083 | return MacroDefinition(dyn_cast_or_null<DefMacroDirective>(MD), |
1084 | S.getActiveModuleMacros(*this, II), |
1085 | S.isAmbiguous(*this, II)); |
1086 | } |
1087 | |
1088 | MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, |
1089 | SourceLocation Loc) { |
1090 | if (!II->hadMacroDefinition()) |
| 5 | | Assuming the condition is false | |
|
| |
1091 | return {}; |
1092 | |
1093 | MacroState &S = CurSubmoduleState->Macros[II]; |
1094 | MacroDirective::DefInfo DI; |
1095 | if (auto *MD = S.getLatest()) |
| |
| |
1096 | DI = MD->findDirectiveAtLoc(Loc, getSourceManager()); |
1097 | |
1098 | return MacroDefinition(DI.getDirective(), |
1099 | S.getActiveModuleMacros(*this, II), |
| 9 | | Calling 'MacroState::getActiveModuleMacros' | |
|
1100 | S.isAmbiguous(*this, II)); |
1101 | } |
1102 | |
1103 | |
1104 | |
1105 | MacroDirective *getLocalMacroDirective(const IdentifierInfo *II) const { |
1106 | if (!II->hasMacroDefinition()) |
1107 | return nullptr; |
1108 | |
1109 | auto *MD = getLocalMacroDirectiveHistory(II); |
1110 | if (!MD || MD->getDefinition().isUndefined()) |
1111 | return nullptr; |
1112 | |
1113 | return MD; |
1114 | } |
1115 | |
1116 | const MacroInfo *getMacroInfo(const IdentifierInfo *II) const { |
1117 | return const_cast<Preprocessor*>(this)->getMacroInfo(II); |
1118 | } |
1119 | |
1120 | MacroInfo *getMacroInfo(const IdentifierInfo *II) { |
1121 | if (!II->hasMacroDefinition()) |
1122 | return nullptr; |
1123 | if (auto MD = getMacroDefinition(II)) |
1124 | return MD.getMacroInfo(); |
1125 | return nullptr; |
1126 | } |
1127 | |
1128 | |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | MacroDirective *getLocalMacroDirectiveHistory(const IdentifierInfo *II) const; |
1134 | |
1135 | |
1136 | void appendMacroDirective(IdentifierInfo *II, MacroDirective *MD); |
1137 | DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, MacroInfo *MI, |
1138 | SourceLocation Loc) { |
1139 | DefMacroDirective *MD = AllocateDefMacroDirective(MI, Loc); |
1140 | appendMacroDirective(II, MD); |
1141 | return MD; |
1142 | } |
1143 | DefMacroDirective *appendDefMacroDirective(IdentifierInfo *II, |
1144 | MacroInfo *MI) { |
1145 | return appendDefMacroDirective(II, MI, MI->getDefinitionLoc()); |
1146 | } |
1147 | |
1148 | |
1149 | void setLoadedMacroDirective(IdentifierInfo *II, MacroDirective *ED, |
1150 | MacroDirective *MD); |
1151 | |
1152 | |
1153 | ModuleMacro *addModuleMacro(Module *Mod, IdentifierInfo *II, MacroInfo *Macro, |
1154 | ArrayRef<ModuleMacro *> Overrides, bool &IsNew); |
1155 | ModuleMacro *getModuleMacro(Module *Mod, const IdentifierInfo *II); |
1156 | |
1157 | |
1158 | ArrayRef<ModuleMacro*> getLeafModuleMacros(const IdentifierInfo *II) const { |
1159 | if (II->isOutOfDate()) |
1160 | updateOutOfDateIdentifier(const_cast<IdentifierInfo&>(*II)); |
1161 | auto I = LeafModuleMacros.find(II); |
1162 | if (I != LeafModuleMacros.end()) |
1163 | return I->second; |
1164 | return None; |
1165 | } |
1166 | |
1167 | |
1168 | ArrayRef<BuildingSubmoduleInfo> getBuildingSubmodules() const { |
1169 | return BuildingSubmoduleStack; |
1170 | } |
1171 | |
1172 | |
1173 | |
1174 | |
1175 | |
1176 | using macro_iterator = MacroMap::const_iterator; |
1177 | |
1178 | macro_iterator macro_begin(bool IncludeExternalMacros = true) const; |
1179 | macro_iterator macro_end(bool IncludeExternalMacros = true) const; |
1180 | |
1181 | llvm::iterator_range<macro_iterator> |
1182 | macros(bool IncludeExternalMacros = true) const { |
1183 | macro_iterator begin = macro_begin(IncludeExternalMacros); |
1184 | macro_iterator end = macro_end(IncludeExternalMacros); |
1185 | return llvm::make_range(begin, end); |
1186 | } |
1187 | |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | StringRef getLastMacroWithSpelling(SourceLocation Loc, |
1194 | ArrayRef<TokenValue> Tokens) const; |
1195 | |
1196 | const std::string &getPredefines() const { return Predefines; } |
1197 | |
1198 | |
1199 | |
1200 | |
1201 | void setPredefines(const char *P) { Predefines = P; } |
1202 | void setPredefines(StringRef P) { Predefines = std::string(P); } |
1203 | |
1204 | |
1205 | |
1206 | IdentifierInfo *getIdentifierInfo(StringRef Name) const { |
1207 | return &Identifiers.get(Name); |
1208 | } |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | void AddPragmaHandler(StringRef Namespace, PragmaHandler *Handler); |
1215 | void AddPragmaHandler(PragmaHandler *Handler) { |
1216 | AddPragmaHandler(StringRef(), Handler); |
1217 | } |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | void RemovePragmaHandler(StringRef Namespace, PragmaHandler *Handler); |
1225 | void RemovePragmaHandler(PragmaHandler *Handler) { |
1226 | RemovePragmaHandler(StringRef(), Handler); |
1227 | } |
1228 | |
1229 | |
1230 | void IgnorePragmas(); |
1231 | |
1232 | |
1233 | void setEmptylineHandler(EmptylineHandler *Handler) { Emptyline = Handler; } |
1234 | |
1235 | EmptylineHandler *getEmptylineHandler() const { return Emptyline; } |
1236 | |
1237 | |
1238 | void addCommentHandler(CommentHandler *Handler); |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | void removeCommentHandler(CommentHandler *Handler); |
1244 | |
1245 | |
1246 | void setCodeCompletionHandler(CodeCompletionHandler &Handler) { |
1247 | CodeComplete = &Handler; |
1248 | } |
1249 | |
1250 | |
1251 | CodeCompletionHandler *getCodeCompletionHandler() const { |
1252 | return CodeComplete; |
1253 | } |
1254 | |
1255 | |
1256 | void clearCodeCompletionHandler() { |
1257 | CodeComplete = nullptr; |
1258 | } |
1259 | |
1260 | |
1261 | |
1262 | void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled); |
1263 | |
1264 | |
1265 | |
1266 | void CodeCompleteNaturalLanguage(); |
1267 | |
1268 | |
1269 | void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter) { |
1270 | CodeCompletionII = Filter; |
1271 | } |
1272 | |
1273 | |
1274 | |
1275 | void setCodeCompletionTokenRange(const SourceLocation Start, |
1276 | const SourceLocation End) { |
1277 | CodeCompletionTokenRange = {Start, End}; |
1278 | } |
1279 | SourceRange getCodeCompletionTokenRange() const { |
1280 | return CodeCompletionTokenRange; |
1281 | } |
1282 | |
1283 | |
1284 | StringRef getCodeCompletionFilter() { |
1285 | if (CodeCompletionII) |
1286 | return CodeCompletionII->getName(); |
1287 | return {}; |
1288 | } |
1289 | |
1290 | |
1291 | |
1292 | PreprocessingRecord *getPreprocessingRecord() const { return Record; } |
1293 | |
1294 | |
1295 | |
1296 | void createPreprocessingRecord(); |
1297 | |
1298 | |
1299 | bool isPCHThroughHeader(const FileEntry *FE); |
1300 | |
1301 | |
1302 | bool creatingPCHWithThroughHeader(); |
1303 | |
1304 | |
1305 | bool usingPCHWithThroughHeader(); |
1306 | |
1307 | |
1308 | bool creatingPCHWithPragmaHdrStop(); |
1309 | |
1310 | |
1311 | bool usingPCHWithPragmaHdrStop(); |
1312 | |
1313 | |
1314 | |
1315 | void SkipTokensWhileUsingPCH(); |
1316 | |
1317 | |
1318 | |
1319 | void HandleSkippedDirectiveWhileUsingPCH(Token &Result, |
1320 | SourceLocation HashLoc); |
1321 | |
1322 | |
1323 | |
1324 | void EnterMainSourceFile(); |
1325 | |
1326 | |
1327 | void EndSourceFile(); |
1328 | |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | bool EnterSourceFile(FileID FID, const DirectoryLookup *Dir, |
1334 | SourceLocation Loc); |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | |
1341 | |
1342 | void EnterMacro(Token &Tok, SourceLocation ILEnd, MacroInfo *Macro, |
1343 | MacroArgs *Args); |
1344 | |
1345 | private: |
1346 | |
1347 | |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | |
1360 | void EnterTokenStream(const Token *Toks, unsigned NumToks, |
1361 | bool DisableMacroExpansion, bool OwnsTokens, |
1362 | bool IsReinject); |
1363 | |
1364 | public: |
1365 | void EnterTokenStream(std::unique_ptr<Token[]> Toks, unsigned NumToks, |
1366 | bool DisableMacroExpansion, bool IsReinject) { |
1367 | EnterTokenStream(Toks.release(), NumToks, DisableMacroExpansion, true, |
1368 | IsReinject); |
1369 | } |
1370 | |
1371 | void EnterTokenStream(ArrayRef<Token> Toks, bool DisableMacroExpansion, |
1372 | bool IsReinject) { |
1373 | EnterTokenStream(Toks.data(), Toks.size(), DisableMacroExpansion, false, |
1374 | IsReinject); |
1375 | } |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | |
1381 | void RemoveTopOfLexerStack(); |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | |
1395 | |
1396 | void EnableBacktrackAtThisPos(); |
1397 | |
1398 | |
1399 | void CommitBacktrackedTokens(); |
1400 | |
1401 | |
1402 | |
1403 | void Backtrack(); |
1404 | |
1405 | |
1406 | |
1407 | bool isBacktrackEnabled() const { return !BacktrackPositions.empty(); } |
1408 | |
1409 | |
1410 | void Lex(Token &Result); |
1411 | |
1412 | |
1413 | bool LexHeaderName(Token &Result, bool AllowMacroExpansion = true); |
1414 | |
1415 | bool LexAfterModuleImport(Token &Result); |
1416 | void CollectPpImportSuffix(SmallVectorImpl<Token> &Toks); |
1417 | |
1418 | void makeModuleVisible(Module *M, SourceLocation Loc); |
1419 | |
1420 | SourceLocation getModuleImportLoc(Module *M) const { |
1421 | return CurSubmoduleState->VisibleModules.getImportLoc(M); |
1422 | } |
1423 | |
1424 | |
1425 | |
1426 | |
1427 | bool LexStringLiteral(Token &Result, std::string &String, |
1428 | const char *DiagnosticTag, bool AllowMacroExpansion) { |
1429 | if (AllowMacroExpansion) |
1430 | Lex(Result); |
1431 | else |
1432 | LexUnexpandedToken(Result); |
1433 | return FinishLexStringLiteral(Result, String, DiagnosticTag, |
1434 | AllowMacroExpansion); |
1435 | } |
1436 | |
1437 | |
1438 | |
1439 | bool FinishLexStringLiteral(Token &Result, std::string &String, |
1440 | const char *DiagnosticTag, |
1441 | bool AllowMacroExpansion); |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | |
1448 | void LexNonComment(Token &Result) { |
1449 | do |
1450 | Lex(Result); |
1451 | while (Result.getKind() == tok::comment); |
1452 | } |
1453 | |
1454 | |
1455 | void LexUnexpandedToken(Token &Result) { |
1456 | |
1457 | bool OldVal = DisableMacroExpansion; |
1458 | DisableMacroExpansion = true; |
1459 | |
1460 | Lex(Result); |
1461 | |
1462 | |
1463 | DisableMacroExpansion = OldVal; |
1464 | } |
1465 | |
1466 | |
1467 | |
1468 | void LexUnexpandedNonComment(Token &Result) { |
1469 | do |
1470 | LexUnexpandedToken(Result); |
1471 | while (Result.getKind() == tok::comment); |
1472 | } |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value); |
1478 | |
1479 | |
1480 | void SetMacroExpansionOnlyInDirectives() { |
1481 | DisableMacroExpansion = true; |
1482 | MacroExpansionInDirectivesOverride = true; |
1483 | } |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | |
1491 | |
1492 | const Token &LookAhead(unsigned N) { |
1493 | assert(LexLevel == 0 && "cannot use lookahead while lexing"); |
1494 | if (CachedLexPos + N < CachedTokens.size()) |
1495 | return CachedTokens[CachedLexPos+N]; |
1496 | else |
1497 | return PeekAhead(N+1); |
1498 | } |
1499 | |
1500 | |
1501 | |
1502 | |
1503 | |
1504 | |
1505 | void RevertCachedTokens(unsigned N) { |
1506 | assert(isBacktrackEnabled() && |
1507 | "Should only be called when tokens are cached for backtracking"); |
1508 | assert(signed(CachedLexPos) - signed(N) >= signed(BacktrackPositions.back()) |
1509 | && "Should revert tokens up to the last backtrack position, not more"); |
1510 | assert(signed(CachedLexPos) - signed(N) >= 0 && |
1511 | "Corrupted backtrack positions ?"); |
1512 | CachedLexPos -= N; |
1513 | } |
1514 | |
1515 | |
1516 | |
1517 | |
1518 | |
1519 | |
1520 | |
1521 | void EnterToken(const Token &Tok, bool IsReinject) { |
1522 | if (LexLevel) { |
1523 | |
1524 | |
1525 | auto TokCopy = std::make_unique<Token[]>(1); |
1526 | TokCopy[0] = Tok; |
1527 | EnterTokenStream(std::move(TokCopy), 1, true, IsReinject); |
1528 | } else { |
1529 | EnterCachingLexMode(); |
1530 | assert(IsReinject && "new tokens in the middle of cached stream"); |
1531 | CachedTokens.insert(CachedTokens.begin()+CachedLexPos, Tok); |
1532 | } |
1533 | } |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | |
1539 | |
1540 | |
1541 | |
1542 | |
1543 | void AnnotateCachedTokens(const Token &Tok) { |
1544 | assert(Tok.isAnnotation() && "Expected annotation token"); |
1545 | if (CachedLexPos != 0 && isBacktrackEnabled()) |
1546 | AnnotatePreviousCachedTokens(Tok); |
1547 | } |
1548 | |
1549 | |
1550 | |
1551 | SourceLocation getLastCachedTokenLocation() const { |
1552 | assert(CachedLexPos != 0); |
1553 | return CachedTokens[CachedLexPos-1].getLastLoc(); |
1554 | } |
1555 | |
1556 | |
1557 | |
1558 | bool IsPreviousCachedToken(const Token &Tok) const; |
1559 | |
1560 | |
1561 | |
1562 | |
1563 | |
1564 | |
1565 | void ReplacePreviousCachedToken(ArrayRef<Token> NewToks); |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | void ReplaceLastTokenWithAnnotation(const Token &Tok) { |
1576 | assert(Tok.isAnnotation() && "Expected annotation token"); |
1577 | if (CachedLexPos != 0 && isBacktrackEnabled()) |
1578 | CachedTokens[CachedLexPos-1] = Tok; |
1579 | } |
1580 | |
1581 | |
1582 | void EnterAnnotationToken(SourceRange Range, tok::TokenKind Kind, |
1583 | void *AnnotationVal); |
1584 | |
1585 | |
1586 | |
1587 | bool mightHavePendingAnnotationTokens() { |
1588 | return CurLexerKind != CLK_Lexer; |
1589 | } |
1590 | |
1591 | |
1592 | |
1593 | void TypoCorrectToken(const Token &Tok) { |
1594 | assert(Tok.getIdentifierInfo() && "Expected identifier token"); |
1595 | if (CachedLexPos != 0 && isBacktrackEnabled()) |
1596 | CachedTokens[CachedLexPos-1] = Tok; |
1597 | } |
1598 | |
1599 | |
1600 | |
1601 | void recomputeCurLexerKind(); |
1602 | |
1603 | |
1604 | bool isIncrementalProcessingEnabled() const { return IncrementalProcessing; } |
1605 | |
1606 | |
1607 | void enableIncrementalProcessing(bool value = true) { |
1608 | IncrementalProcessing = value; |
1609 | } |
1610 | |
1611 | |
1612 | |
1613 | |
1614 | |
1615 | |
1616 | |
1617 | |
1618 | |
1619 | |
1620 | |
1621 | |
1622 | |
1623 | |
1624 | |
1625 | bool SetCodeCompletionPoint(const FileEntry *File, |
1626 | unsigned Line, unsigned Column); |
1627 | |
1628 | |
1629 | bool isCodeCompletionEnabled() const { return CodeCompletionFile != nullptr; } |
1630 | |
1631 | |
1632 | |
1633 | |
1634 | |
1635 | SourceLocation getCodeCompletionLoc() const { return CodeCompletionLoc; } |
1636 | |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | SourceLocation getCodeCompletionFileLoc() const { |
1642 | return CodeCompletionFileLoc; |
1643 | } |
1644 | |
1645 | |
1646 | |
1647 | bool isCodeCompletionReached() const { return CodeCompletionReached; } |
1648 | |
1649 | |
1650 | void setCodeCompletionReached() { |
1651 | assert(isCodeCompletionEnabled() && "Code-completion not enabled!"); |
1652 | CodeCompletionReached = true; |
1653 | |
1654 | getDiagnostics().setSuppressAllDiagnostics(true); |
1655 | } |
1656 | |
1657 | |
1658 | |
1659 | |
1660 | |
1661 | std::pair<IdentifierInfo *, SourceLocation> |
1662 | getPragmaARCCFCodeAuditedInfo() const { |
1663 | return PragmaARCCFCodeAuditedInfo; |
1664 | } |
1665 | |
1666 | |
1667 | |
1668 | void setPragmaARCCFCodeAuditedInfo(IdentifierInfo *Ident, |
1669 | SourceLocation Loc) { |
1670 | PragmaARCCFCodeAuditedInfo = {Ident, Loc}; |
1671 | } |
1672 | |
1673 | |
1674 | |
1675 | |
1676 | |
1677 | SourceLocation getPragmaAssumeNonNullLoc() const { |
1678 | return PragmaAssumeNonNullLoc; |
1679 | } |
1680 | |
1681 | |
1682 | |
1683 | void setPragmaAssumeNonNullLoc(SourceLocation Loc) { |
1684 | PragmaAssumeNonNullLoc = Loc; |
1685 | } |
1686 | |
1687 | |
1688 | |
1689 | void setMainFileDir(const DirectoryEntry *Dir) { |
1690 | MainFileDir = Dir; |
1691 | } |
1692 | |
1693 | |
1694 | |
1695 | |
1696 | |
1697 | |
1698 | |
1699 | void setSkipMainFilePreamble(unsigned Bytes, bool StartOfLine) { |
1700 | SkipMainFilePreamble.first = Bytes; |
1701 | SkipMainFilePreamble.second = StartOfLine; |
1702 | } |
1703 | |
1704 | |
1705 | |
1706 | |
1707 | DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const { |
1708 | return Diags->Report(Loc, DiagID); |
1709 | } |
1710 | |
1711 | DiagnosticBuilder Diag(const Token &Tok, unsigned DiagID) const { |
1712 | return Diags->Report(Tok.getLocation(), DiagID); |
1713 | } |
1714 | |
1715 | |
1716 | |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | |
1722 | StringRef getSpelling(SourceLocation loc, |
1723 | SmallVectorImpl<char> &buffer, |
1724 | bool *invalid = nullptr) const { |
1725 | return Lexer::getSpelling(loc, buffer, SourceMgr, LangOpts, invalid); |
1726 | } |
1727 | |
1728 | |
1729 | |
1730 | |
1731 | |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | std::string getSpelling(const Token &Tok, bool *Invalid = nullptr) const { |
1737 | return Lexer::getSpelling(Tok, SourceMgr, LangOpts, Invalid); |
1738 | } |
1739 | |
1740 | |
1741 | |
1742 | |
1743 | |
1744 | |
1745 | |
1746 | |
1747 | |
1748 | |
1749 | |
1750 | |
1751 | |
1752 | unsigned getSpelling(const Token &Tok, const char *&Buffer, |
1753 | bool *Invalid = nullptr) const { |
1754 | return Lexer::getSpelling(Tok, Buffer, SourceMgr, LangOpts, Invalid); |
1755 | } |
1756 | |
1757 | |
1758 | |
1759 | |
1760 | |
1761 | StringRef getSpelling(const Token &Tok, |
1762 | SmallVectorImpl<char> &Buffer, |
1763 | bool *Invalid = nullptr) const; |
1764 | |
1765 | |
1766 | |
1767 | bool getRawToken(SourceLocation Loc, Token &Result, |
1768 | bool IgnoreWhiteSpace = false) { |
1769 | return Lexer::getRawToken(Loc, Result, SourceMgr, LangOpts, IgnoreWhiteSpace); |
1770 | } |
1771 | |
1772 | |
1773 | |
1774 | char |
1775 | getSpellingOfSingleCharacterNumericConstant(const Token &Tok, |
1776 | bool *Invalid = nullptr) const { |
1777 | assert(Tok.is(tok::numeric_constant) && |
1778 | Tok.getLength() == 1 && "Called on unsupported token"); |
1779 | assert(!Tok.needsCleaning() && "Token can't need cleaning with length 1"); |
1780 | |
1781 | |
1782 | if (const char *D = Tok.getLiteralData()) |
1783 | return *D; |
1784 | |
1785 | |
1786 | |
1787 | return *SourceMgr.getCharacterData(Tok.getLocation(), Invalid); |
1788 | } |
1789 | |
1790 | |
1791 | |
1792 | |
1793 | |
1794 | |
1795 | |
1796 | |
1797 | |
1798 | StringRef getImmediateMacroName(SourceLocation Loc) { |
1799 | return Lexer::getImmediateMacroName(Loc, SourceMgr, getLangOpts()); |
1800 | } |
1801 | |
1802 | |
1803 | |
1804 | |
1805 | |
1806 | |
1807 | void CreateString(StringRef Str, Token &Tok, |
1808 | SourceLocation ExpansionLocStart = SourceLocation(), |
1809 | SourceLocation ExpansionLocEnd = SourceLocation()); |
1810 | |
1811 | |
1812 | |
1813 | |
1814 | SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length); |
1815 | |
1816 | |
1817 | |
1818 | |
1819 | |
1820 | |
1821 | |
1822 | |
1823 | |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | |
1829 | |
1830 | |
1831 | SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset = 0) { |
1832 | return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts); |
1833 | } |
1834 | |
1835 | |
1836 | |
1837 | |
1838 | |
1839 | |
1840 | bool isAtStartOfMacroExpansion(SourceLocation loc, |
1841 | SourceLocation *MacroBegin = nullptr) const { |
1842 | return Lexer::isAtStartOfMacroExpansion(loc, SourceMgr, LangOpts, |
1843 | MacroBegin); |
1844 | } |
1845 | |
1846 | |
1847 | |
1848 | |
1849 | |
1850 | |
1851 | bool isAtEndOfMacroExpansion(SourceLocation loc, |
1852 | SourceLocation *MacroEnd = nullptr) const { |
1853 | return Lexer::isAtEndOfMacroExpansion(loc, SourceMgr, LangOpts, MacroEnd); |
1854 | } |
1855 | |
1856 | |
1857 | void DumpToken(const Token &Tok, bool DumpFlags = false) const; |
1858 | void DumpLocation(SourceLocation Loc) const; |
1859 | void DumpMacro(const MacroInfo &MI) const; |
1860 | void dumpMacroInfo(const IdentifierInfo *II); |
1861 | |
1862 | |
1863 | |
1864 | SourceLocation AdvanceToTokenCharacter(SourceLocation TokStart, |
1865 | unsigned Char) const { |
1866 | return Lexer::AdvanceToTokenCharacter(TokStart, Char, SourceMgr, LangOpts); |
1867 | } |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | void IncrementPasteCounter(bool isFast) { |
1874 | if (isFast) |
1875 | ++NumFastTokenPaste; |
1876 | else |
1877 | ++NumTokenPaste; |
1878 | } |
1879 | |
1880 | void PrintStats(); |
1881 | |
1882 | size_t getTotalMemory() const; |
1883 | |
1884 | |
1885 | |
1886 | |
1887 | void HandleMicrosoftCommentPaste(Token &Tok); |
1888 | |
1889 | |
1890 | |
1891 | |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | IdentifierInfo *LookUpIdentifierInfo(Token &Identifier) const; |
1897 | |
1898 | private: |
1899 | llvm::DenseMap<IdentifierInfo*,unsigned> PoisonReasons; |
1900 | |
1901 | public: |
1902 | |
1903 | |
1904 | |
1905 | |
1906 | void SetPoisonReason(IdentifierInfo *II, unsigned DiagID); |
1907 | |
1908 | |
1909 | void HandlePoisonedIdentifier(Token & Identifier); |
1910 | |
1911 | void MaybeHandlePoisonedIdentifier(Token & Identifier) { |
1912 | if(IdentifierInfo * II = Identifier.getIdentifierInfo()) { |
1913 | if(II->isPoisoned()) { |
1914 | HandlePoisonedIdentifier(Identifier); |
1915 | } |
1916 | } |
1917 | } |
1918 | |
1919 | private: |
1920 | |
1921 | |
1922 | |
1923 | IdentifierInfo *Ident__exception_code, |
1924 | *Ident___exception_code, |
1925 | *Ident_GetExceptionCode; |
1926 | |
1927 | IdentifierInfo *Ident__exception_info, |
1928 | *Ident___exception_info, |
1929 | *Ident_GetExceptionInfo; |
1930 | |
1931 | IdentifierInfo *Ident__abnormal_termination, |
1932 | *Ident___abnormal_termination, |
1933 | *Ident_AbnormalTermination; |
1934 | |
1935 | const char *getCurLexerEndPos(); |
1936 | void diagnoseMissingHeaderInUmbrellaDir(const Module &Mod); |
1937 | |
1938 | public: |
1939 | void PoisonSEHIdentifiers(bool Poison = true); |
1940 | |
1941 | |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | bool HandleIdentifier(Token &Identifier); |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | |
1955 | |
1956 | bool HandleEndOfFile(Token &Result, SourceLocation Loc, |
1957 | bool isEndOfMacro = false); |
1958 | |
1959 | |
1960 | |
1961 | bool HandleEndOfTokenLexer(Token &Result); |
1962 | |
1963 | |
1964 | |
1965 | |
1966 | |
1967 | |
1968 | void HandleDirective(Token &Result); |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | |
1976 | |
1977 | |
1978 | SourceLocation CheckEndOfDirective(const char *DirType, |
1979 | bool EnableMacros = false); |
1980 | |
1981 | |
1982 | |
1983 | SourceRange DiscardUntilEndOfDirective(); |
1984 | |
1985 | |
1986 | |
1987 | bool SawDateOrTime() const { |
1988 | return DATELoc != SourceLocation() || TIMELoc != SourceLocation(); |
1989 | } |
1990 | unsigned getCounterValue() const { return CounterValue; } |
1991 | void setCounterValue(unsigned V) { CounterValue = V; } |
1992 | |
1993 | |
1994 | Module *getCurrentModule(); |
1995 | |
1996 | |
1997 | MacroInfo *AllocateMacroInfo(SourceLocation L); |
1998 | |
1999 | |
2000 | |
2001 | |
2002 | |
2003 | |
2004 | |
2005 | |
2006 | |
2007 | |
2008 | bool GetIncludeFilenameSpelling(SourceLocation Loc,StringRef &Buffer); |
2009 | |
2010 | |
2011 | |
2012 | |
2013 | |
2014 | Optional<FileEntryRef> |
2015 | LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, |
2016 | const DirectoryLookup *FromDir, const FileEntry *FromFile, |
2017 | const DirectoryLookup *&CurDir, SmallVectorImpl<char> *SearchPath, |
2018 | SmallVectorImpl<char> *RelativePath, |
2019 | ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, |
2020 | bool *IsFrameworkFound, bool SkipCache = false); |
2021 | |
2022 | |
2023 | |
2024 | |
2025 | |
2026 | |
2027 | const DirectoryLookup *GetCurDirLookup() { return CurDirLookup; } |
2028 | |
2029 | |
2030 | bool isInPrimaryFile() const; |
2031 | |
2032 | |
2033 | |
2034 | bool LexOnOffSwitch(tok::OnOffSwitch &Result); |
2035 | |
2036 | bool CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, |
2037 | bool *ShadowFlag = nullptr); |
2038 | |
2039 | void EnterSubmodule(Module *M, SourceLocation ImportLoc, bool ForPragma); |
2040 | Module *LeaveSubmodule(bool ForPragma); |
2041 | |
2042 | private: |
2043 | friend void TokenLexer::ExpandFunctionArguments(); |
2044 | |
2045 | void PushIncludeMacroStack() { |
2046 | assert(CurLexerKind != CLK_CachingLexer && "cannot push a caching lexer"); |
2047 | IncludeMacroStack.emplace_back(CurLexerKind, CurLexerSubmodule, |
2048 | std::move(CurLexer), CurPPLexer, |
2049 | std::move(CurTokenLexer), CurDirLookup); |
2050 | CurPPLexer = nullptr; |
2051 | } |
2052 | |
2053 | void PopIncludeMacroStack() { |
2054 | CurLexer = std::move(IncludeMacroStack.back().TheLexer); |
2055 | CurPPLexer = IncludeMacroStack.back().ThePPLexer; |
2056 | CurTokenLexer = std::move(IncludeMacroStack.back().TheTokenLexer); |
2057 | CurDirLookup = IncludeMacroStack.back().TheDirLookup; |
2058 | CurLexerSubmodule = IncludeMacroStack.back().TheSubmodule; |
2059 | CurLexerKind = IncludeMacroStack.back().CurLexerKind; |
2060 | IncludeMacroStack.pop_back(); |
2061 | } |
2062 | |
2063 | void PropagateLineStartLeadingSpaceInfo(Token &Result); |
2064 | |
2065 | |
2066 | |
2067 | bool needModuleMacros() const; |
2068 | |
2069 | |
2070 | |
2071 | void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info); |
2072 | |
2073 | DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI, |
2074 | SourceLocation Loc); |
2075 | UndefMacroDirective *AllocateUndefMacroDirective(SourceLocation UndefLoc); |
2076 | VisibilityMacroDirective *AllocateVisibilityMacroDirective(SourceLocation Loc, |
2077 | bool isPublic); |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | |
2087 | |
2088 | |
2089 | void ReadMacroName(Token &MacroNameTok, MacroUse IsDefineUndef = MU_Other, |
2090 | bool *ShadowFlag = nullptr); |
2091 | |
2092 | |
2093 | |
2094 | |
2095 | |
2096 | |
2097 | |
2098 | |
2099 | |
2100 | |
2101 | MacroInfo *ReadOptionalMacroParameterListAndBody( |
2102 | const Token &MacroNameTok, bool ImmediatelyAfterHeaderGuard); |
2103 | |
2104 | |
2105 | |
2106 | |
2107 | |
2108 | bool ReadMacroParameterList(MacroInfo *MI, Token& LastTok); |
2109 | |
2110 | |
2111 | |
2112 | |
2113 | |
2114 | |
2115 | |
2116 | |
2117 | |
2118 | void SkipExcludedConditionalBlock(SourceLocation HashTokenLoc, |
2119 | SourceLocation IfTokenLoc, |
2120 | bool FoundNonSkipPortion, bool FoundElse, |
2121 | SourceLocation ElseLoc = SourceLocation()); |
2122 | |
2123 | |
2124 | |
2125 | struct DirectiveEvalResult { |
2126 | |
2127 | bool Conditional; |
2128 | |
2129 | |
2130 | bool IncludedUndefinedIds; |
2131 | |
2132 | |
2133 | SourceRange ExprRange; |
2134 | }; |
2135 | |
2136 | |
2137 | |
2138 | |
2139 | |
2140 | DirectiveEvalResult EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro); |
2141 | |
2142 | |
2143 | |
2144 | void RegisterBuiltinPragmas(); |
2145 | |
2146 | |
2147 | void RegisterBuiltinMacros(); |
2148 | |
2149 | |
2150 | |
2151 | |
2152 | bool HandleMacroExpandedIdentifier(Token &Identifier, const MacroDefinition &MD); |
2153 | |
2154 | |
2155 | |
2156 | |
2157 | |
2158 | |
2159 | Token *cacheMacroExpandedTokens(TokenLexer *tokLexer, |
2160 | ArrayRef<Token> tokens); |
2161 | |
2162 | void removeCachedMacroExpandedTokensOfLastLexer(); |
2163 | |
2164 | |
2165 | |
2166 | |
2167 | bool isNextPPTokenLParen(); |
2168 | |
2169 | |
2170 | |
2171 | MacroArgs *ReadMacroCallArgumentList(Token &MacroName, MacroInfo *MI, |
2172 | SourceLocation &MacroEnd); |
2173 | |
2174 | |
2175 | |
2176 | void ExpandBuiltinMacro(Token &Tok); |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | void Handle_Pragma(Token &Tok); |
2182 | |
2183 | |
2184 | |
2185 | void HandleMicrosoft__pragma(Token &Tok); |
2186 | |
2187 | |
2188 | |
2189 | void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir); |
2190 | |
2191 | |
2192 | void setPredefinesFileID(FileID FID) { |
2193 | assert(PredefinesFileID.isInvalid() && "PredefinesFileID already set!"); |
2194 | PredefinesFileID = FID; |
2195 | } |
2196 | |
2197 | |
2198 | void setPCHThroughHeaderFileID(FileID FID); |
2199 | |
2200 | |
2201 | |
2202 | static bool IsFileLexer(const Lexer* L, const PreprocessorLexer* P) { |
2203 | return L ? !L->isPragmaLexer() : P != nullptr; |
2204 | } |
2205 | |
2206 | static bool IsFileLexer(const IncludeStackInfo& I) { |
2207 | return IsFileLexer(I.TheLexer.get(), I.ThePPLexer); |
2208 | } |
2209 | |
2210 | bool IsFileLexer() const { |
2211 | return IsFileLexer(CurLexer.get(), CurPPLexer); |
2212 | } |
2213 | |
2214 | |
2215 | |
2216 | void CachingLex(Token &Result); |
2217 | |
2218 | bool InCachingLexMode() const { |
2219 | |
2220 | |
2221 | return !CurPPLexer && !CurTokenLexer && !IncludeMacroStack.empty(); |
2222 | } |
2223 | |
2224 | void EnterCachingLexMode(); |
2225 | void EnterCachingLexModeUnchecked(); |
2226 | |
2227 | void ExitCachingLexMode() { |
2228 | if (InCachingLexMode()) |
2229 | RemoveTopOfLexerStack(); |
2230 | } |
2231 | |
2232 | const Token &PeekAhead(unsigned N); |
2233 | void AnnotatePreviousCachedTokens(const Token &Tok); |
2234 | |
2235 | |
2236 | |
2237 | |
2238 | |
2239 | void HandleLineDirective(); |
2240 | void HandleDigitDirective(Token &Tok); |
2241 | void HandleUserDiagnosticDirective(Token &Tok, bool isWarning); |
2242 | void HandleIdentSCCSDirective(Token &Tok); |
2243 | void HandleMacroPublicDirective(Token &Tok); |
2244 | void HandleMacroPrivateDirective(); |
2245 | |
2246 | |
2247 | |
2248 | struct ImportAction { |
2249 | enum ActionKind { |
2250 | None, |
2251 | ModuleBegin, |
2252 | ModuleImport, |
2253 | SkippedModuleImport, |
2254 | Failure, |
2255 | } Kind; |
2256 | Module *ModuleForHeader = nullptr; |
2257 | |
2258 | ImportAction(ActionKind AK, Module *Mod = nullptr) |
2259 | : Kind(AK), ModuleForHeader(Mod) { |
2260 | assert((AK == None || Mod || AK == Failure) && |
2261 | "no module for module action"); |
2262 | } |
2263 | }; |
2264 | |
2265 | Optional<FileEntryRef> LookupHeaderIncludeOrImport( |
2266 | const DirectoryLookup *&CurDir, StringRef &Filename, |
2267 | SourceLocation FilenameLoc, CharSourceRange FilenameRange, |
2268 | const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl, |
2269 | bool &IsMapped, const DirectoryLookup *LookupFrom, |
2270 | const FileEntry *LookupFromFile, StringRef &LookupFilename, |
2271 | SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath, |
2272 | ModuleMap::KnownHeader &SuggestedModule, bool isAngled); |
2273 | |
2274 | |
2275 | void HandleIncludeDirective(SourceLocation HashLoc, Token &Tok, |
2276 | const DirectoryLookup *LookupFrom = nullptr, |
2277 | const FileEntry *LookupFromFile = nullptr); |
2278 | ImportAction |
2279 | HandleHeaderIncludeOrImport(SourceLocation HashLoc, Token &IncludeTok, |
2280 | Token &FilenameTok, SourceLocation EndLoc, |
2281 | const DirectoryLookup *LookupFrom = nullptr, |
2282 | const FileEntry *LookupFromFile = nullptr); |
2283 | void HandleIncludeNextDirective(SourceLocation HashLoc, Token &Tok); |
2284 | void HandleIncludeMacrosDirective(SourceLocation HashLoc, Token &Tok); |
2285 | void HandleImportDirective(SourceLocation HashLoc, Token &Tok); |
2286 | void HandleMicrosoftImportDirective(Token &Tok); |
2287 | |
2288 | public: |
2289 | |
2290 | |
2291 | |
2292 | static bool checkModuleIsAvailable(const LangOptions &LangOpts, |
2293 | const TargetInfo &TargetInfo, |
2294 | DiagnosticsEngine &Diags, Module *M); |
2295 | |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | Module *getModuleForLocation(SourceLocation Loc); |
2301 | |
2302 | |
2303 | |
2304 | |
2305 | |
2306 | |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | |
2313 | |
2314 | |
2315 | |
2316 | const FileEntry *getHeaderToIncludeForDiagnostics(SourceLocation IncLoc, |
2317 | SourceLocation MLoc); |
2318 | |
2319 | bool isRecordingPreamble() const { |
2320 | return PreambleConditionalStack.isRecording(); |
2321 | } |
2322 | |
2323 | bool hasRecordedPreamble() const { |
2324 | return PreambleConditionalStack.hasRecordedPreamble(); |
2325 | } |
2326 | |
2327 | ArrayRef<PPConditionalInfo> getPreambleConditionalStack() const { |
2328 | return PreambleConditionalStack.getStack(); |
2329 | } |
2330 | |
2331 | void setRecordedPreambleConditionalStack(ArrayRef<PPConditionalInfo> s) { |
2332 | PreambleConditionalStack.setStack(s); |
2333 | } |
2334 | |
2335 | void setReplayablePreambleConditionalStack(ArrayRef<PPConditionalInfo> s, |
2336 | llvm::Optional<PreambleSkipInfo> SkipInfo) { |
2337 | PreambleConditionalStack.startReplaying(); |
2338 | PreambleConditionalStack.setStack(s); |
2339 | PreambleConditionalStack.SkipInfo = SkipInfo; |
2340 | } |
2341 | |
2342 | llvm::Optional<PreambleSkipInfo> getPreambleSkipInfo() const { |
2343 | return PreambleConditionalStack.SkipInfo; |
2344 | } |
2345 | |
2346 | private: |
2347 | |
2348 | |
2349 | void replayPreambleConditionalStack(); |
2350 | |
2351 | |
2352 | void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard); |
2353 | void HandleUndefDirective(); |
2354 | |
2355 | |
2356 | void HandleIfdefDirective(Token &Result, const Token &HashToken, |
2357 | bool isIfndef, bool ReadAnyTokensBeforeDirective); |
2358 | void HandleIfDirective(Token &IfToken, const Token &HashToken, |
2359 | bool ReadAnyTokensBeforeDirective); |
2360 | void HandleEndifDirective(Token &EndifToken); |
2361 | void HandleElseDirective(Token &Result, const Token &HashToken); |
2362 | void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken, |
2363 | tok::PPKeywordKind Kind); |
2364 | |
2365 | |
2366 | void HandlePragmaDirective(PragmaIntroducer Introducer); |
2367 | void ResolvePragmaIncludeInstead(SourceLocation Location) const; |
2368 | |
2369 | public: |
2370 | void HandlePragmaOnce(Token &OnceTok); |
2371 | void HandlePragmaMark(Token &MarkTok); |
2372 | void HandlePragmaPoison(); |
2373 | void HandlePragmaSystemHeader(Token &SysHeaderTok); |
2374 | void HandlePragmaIncludeInstead(Token &Tok); |
2375 | void HandlePragmaDependency(Token &DependencyTok); |
2376 | void HandlePragmaPushMacro(Token &Tok); |
2377 | void HandlePragmaPopMacro(Token &Tok); |
2378 | void HandlePragmaIncludeAlias(Token &Tok); |
2379 | void HandlePragmaModuleBuild(Token &Tok); |
2380 | void HandlePragmaHdrstop(Token &Tok); |
2381 | IdentifierInfo *ParsePragmaPushOrPopMacro(Token &Tok); |
2382 | |
2383 | |
2384 | |
2385 | bool HandleComment(Token &result, SourceRange Comment); |
2386 | |
2387 | |
2388 | |
2389 | void markMacroAsUsed(MacroInfo *MI); |
2390 | |
2391 | private: |
2392 | Optional<unsigned> |
2393 | getSkippedRangeForExcludedConditionalBlock(SourceLocation HashLoc); |
2394 | |
2395 | |
2396 | |
2397 | ExcludedPreprocessorDirectiveSkipMapping |
2398 | *ExcludedConditionalDirectiveSkipMappings; |
2399 | }; |
2400 | |
2401 | |
2402 | |
2403 | class CommentHandler { |
2404 | public: |
2405 | virtual ~CommentHandler(); |
2406 | |
2407 | |
2408 | |
2409 | virtual bool HandleComment(Preprocessor &PP, SourceRange Comment) = 0; |
2410 | }; |
2411 | |
2412 | |
2413 | |
2414 | class EmptylineHandler { |
2415 | public: |
2416 | virtual ~EmptylineHandler(); |
2417 | |
2418 | |
2419 | virtual void HandleEmptyline(SourceRange Range) = 0; |
2420 | }; |
2421 | |
2422 | |
2423 | using PragmaHandlerRegistry = llvm::Registry<PragmaHandler>; |
2424 | |
2425 | } |
2426 | |
2427 | #endif // LLVM_CLANG_LEX_PREPROCESSOR_H |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_SUPPORT_ALLOCATOR_H |
18 | #define LLVM_SUPPORT_ALLOCATOR_H |
19 | |
20 | #include "llvm/ADT/Optional.h" |
21 | #include "llvm/ADT/SmallVector.h" |
22 | #include "llvm/Support/Alignment.h" |
23 | #include "llvm/Support/AllocatorBase.h" |
24 | #include "llvm/Support/Compiler.h" |
25 | #include "llvm/Support/ErrorHandling.h" |
26 | #include "llvm/Support/MathExtras.h" |
27 | #include "llvm/Support/MemAlloc.h" |
28 | #include <algorithm> |
29 | #include <cassert> |
30 | #include <cstddef> |
31 | #include <cstdint> |
32 | #include <cstdlib> |
33 | #include <iterator> |
34 | #include <type_traits> |
35 | #include <utility> |
36 | |
37 | namespace llvm { |
38 | |
39 | namespace detail { |
40 | |
41 | |
42 | |
43 | void printBumpPtrAllocatorStats(unsigned NumSlabs, size_t BytesAllocated, |
44 | size_t TotalMemory); |
45 | |
46 | } |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | template <typename AllocatorT = MallocAllocator, size_t SlabSize = 4096, |
66 | size_t SizeThreshold = SlabSize, size_t GrowthDelay = 128> |
67 | class BumpPtrAllocatorImpl |
68 | : public AllocatorBase<BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
69 | SizeThreshold, GrowthDelay>>, |
70 | private AllocatorT { |
71 | public: |
72 | static_assert(SizeThreshold <= SlabSize, |
73 | "The SizeThreshold must be at most the SlabSize to ensure " |
74 | "that objects larger than a slab go into their own memory " |
75 | "allocation."); |
76 | static_assert(GrowthDelay > 0, |
77 | "GrowthDelay must be at least 1 which already increases the" |
78 | "slab size after each allocated slab."); |
79 | |
80 | BumpPtrAllocatorImpl() = default; |
81 | |
82 | template <typename T> |
83 | BumpPtrAllocatorImpl(T &&Allocator) |
84 | : AllocatorT(std::forward<T &&>(Allocator)) {} |
85 | |
86 | |
87 | |
88 | BumpPtrAllocatorImpl(BumpPtrAllocatorImpl &&Old) |
89 | : AllocatorT(static_cast<AllocatorT &&>(Old)), CurPtr(Old.CurPtr), |
90 | End(Old.End), Slabs(std::move(Old.Slabs)), |
91 | CustomSizedSlabs(std::move(Old.CustomSizedSlabs)), |
92 | BytesAllocated(Old.BytesAllocated), RedZoneSize(Old.RedZoneSize) { |
93 | Old.CurPtr = Old.End = nullptr; |
94 | Old.BytesAllocated = 0; |
95 | Old.Slabs.clear(); |
96 | Old.CustomSizedSlabs.clear(); |
97 | } |
98 | |
99 | ~BumpPtrAllocatorImpl() { |
100 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
101 | DeallocateCustomSizedSlabs(); |
102 | } |
103 | |
104 | BumpPtrAllocatorImpl &operator=(BumpPtrAllocatorImpl &&RHS) { |
105 | DeallocateSlabs(Slabs.begin(), Slabs.end()); |
106 | DeallocateCustomSizedSlabs(); |
107 | |
108 | CurPtr = RHS.CurPtr; |
109 | End = RHS.End; |
110 | BytesAllocated = RHS.BytesAllocated; |
111 | RedZoneSize = RHS.RedZoneSize; |
112 | Slabs = std::move(RHS.Slabs); |
113 | CustomSizedSlabs = std::move(RHS.CustomSizedSlabs); |
114 | AllocatorT::operator=(static_cast<AllocatorT &&>(RHS)); |
115 | |
116 | RHS.CurPtr = RHS.End = nullptr; |
117 | RHS.BytesAllocated = 0; |
118 | RHS.Slabs.clear(); |
119 | RHS.CustomSizedSlabs.clear(); |
120 | return *this; |
121 | } |
122 | |
123 | |
124 | |
125 | void Reset() { |
126 | |
127 | DeallocateCustomSizedSlabs(); |
128 | CustomSizedSlabs.clear(); |
129 | |
130 | if (Slabs.empty()) |
131 | return; |
132 | |
133 | |
134 | BytesAllocated = 0; |
135 | CurPtr = (char *)Slabs.front(); |
136 | End = CurPtr + SlabSize; |
137 | |
138 | __asan_poison_memory_region(*Slabs.begin(), computeSlabSize(0)); |
139 | DeallocateSlabs(std::next(Slabs.begin()), Slabs.end()); |
140 | Slabs.erase(std::next(Slabs.begin()), Slabs.end()); |
141 | } |
142 | |
143 | |
144 | LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
145 | Allocate(size_t Size, Align Alignment) { |
146 | |
147 | BytesAllocated += Size; |
148 | |
149 | size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment); |
| 21 | | Calling 'offsetToAlignedAddr' | |
|
150 | assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow"); |
151 | |
152 | size_t SizeToAllocate = Size; |
153 | #if LLVM_ADDRESS_SANITIZER_BUILD |
154 | |
155 | SizeToAllocate += RedZoneSize; |
156 | #endif |
157 | |
158 | |
159 | if (Adjustment + SizeToAllocate <= size_t(End - CurPtr)) { |
160 | char *AlignedPtr = CurPtr + Adjustment; |
161 | CurPtr = AlignedPtr + SizeToAllocate; |
162 | |
163 | |
164 | |
165 | __msan_allocated_memory(AlignedPtr, Size); |
166 | |
167 | __asan_unpoison_memory_region(AlignedPtr, Size); |
168 | return AlignedPtr; |
169 | } |
170 | |
171 | |
172 | size_t PaddedSize = SizeToAllocate + Alignment.value() - 1; |
173 | if (PaddedSize > SizeThreshold) { |
174 | void *NewSlab = |
175 | AllocatorT::Allocate(PaddedSize, alignof(std::max_align_t)); |
176 | |
177 | |
178 | __asan_poison_memory_region(NewSlab, PaddedSize); |
179 | CustomSizedSlabs.push_back(std::make_pair(NewSlab, PaddedSize)); |
180 | |
181 | uintptr_t AlignedAddr = alignAddr(NewSlab, Alignment); |
182 | assert(AlignedAddr + Size <= (uintptr_t)NewSlab + PaddedSize); |
183 | char *AlignedPtr = (char*)AlignedAddr; |
184 | __msan_allocated_memory(AlignedPtr, Size); |
185 | __asan_unpoison_memory_region(AlignedPtr, Size); |
186 | return AlignedPtr; |
187 | } |
188 | |
189 | |
190 | StartNewSlab(); |
191 | uintptr_t AlignedAddr = alignAddr(CurPtr, Alignment); |
192 | assert(AlignedAddr + SizeToAllocate <= (uintptr_t)End && |
193 | "Unable to allocate memory!"); |
194 | char *AlignedPtr = (char*)AlignedAddr; |
195 | CurPtr = AlignedPtr + SizeToAllocate; |
196 | __msan_allocated_memory(AlignedPtr, Size); |
197 | __asan_unpoison_memory_region(AlignedPtr, Size); |
198 | return AlignedPtr; |
199 | } |
200 | |
201 | inline LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * |
202 | Allocate(size_t Size, size_t Alignment) { |
203 | assert(Alignment > 0 && "0-byte alignment is not allowed. Use 1 instead."); |
204 | return Allocate(Size, Align(Alignment)); |
| 20 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
205 | } |
206 | |
207 | |
208 | using AllocatorBase<BumpPtrAllocatorImpl>::Allocate; |
209 | |
210 | |
211 | |
212 | |
213 | void Deallocate(const void *Ptr, size_t Size, size_t ) { |
214 | __asan_poison_memory_region(Ptr, Size); |
215 | } |
216 | |
217 | |
218 | using AllocatorBase<BumpPtrAllocatorImpl>::Deallocate; |
219 | |
220 | size_t GetNumSlabs() const { return Slabs.size() + CustomSizedSlabs.size(); } |
221 | |
222 | |
223 | |
224 | |
225 | |
226 | |
227 | llvm::Optional<int64_t> identifyObject(const void *Ptr) { |
228 | const char *P = static_cast<const char *>(Ptr); |
229 | int64_t InSlabIdx = 0; |
230 | for (size_t Idx = 0, E = Slabs.size(); Idx < E; Idx++) { |
231 | const char *S = static_cast<const char *>(Slabs[Idx]); |
232 | if (P >= S && P < S + computeSlabSize(Idx)) |
233 | return InSlabIdx + static_cast<int64_t>(P - S); |
234 | InSlabIdx += static_cast<int64_t>(computeSlabSize(Idx)); |
235 | } |
236 | |
237 | |
238 | int64_t InCustomSizedSlabIdx = -1; |
239 | for (size_t Idx = 0, E = CustomSizedSlabs.size(); Idx < E; Idx++) { |
240 | const char *S = static_cast<const char *>(CustomSizedSlabs[Idx].first); |
241 | size_t Size = CustomSizedSlabs[Idx].second; |
242 | if (P >= S && P < S + Size) |
243 | return InCustomSizedSlabIdx - static_cast<int64_t>(P - S); |
244 | InCustomSizedSlabIdx -= static_cast<int64_t>(Size); |
245 | } |
246 | return None; |
247 | } |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | int64_t identifyKnownObject(const void *Ptr) { |
254 | Optional<int64_t> Out = identifyObject(Ptr); |
255 | assert(Out && "Wrong allocator used"); |
256 | return *Out; |
257 | } |
258 | |
259 | |
260 | |
261 | |
262 | |
263 | |
264 | |
265 | |
266 | |
267 | |
268 | |
269 | template <typename T> |
270 | int64_t identifyKnownAlignedObject(const void *Ptr) { |
271 | int64_t Out = identifyKnownObject(Ptr); |
272 | assert(Out % alignof(T) == 0 && "Wrong alignment information"); |
273 | return Out / alignof(T); |
274 | } |
275 | |
276 | size_t getTotalMemory() const { |
277 | size_t TotalMemory = 0; |
278 | for (auto I = Slabs.begin(), E = Slabs.end(); I != E; ++I) |
279 | TotalMemory += computeSlabSize(std::distance(Slabs.begin(), I)); |
280 | for (auto &PtrAndSize : CustomSizedSlabs) |
281 | TotalMemory += PtrAndSize.second; |
282 | return TotalMemory; |
283 | } |
284 | |
285 | size_t getBytesAllocated() const { return BytesAllocated; } |
286 | |
287 | void setRedZoneSize(size_t NewSize) { |
288 | RedZoneSize = NewSize; |
289 | } |
290 | |
291 | void PrintStats() const { |
292 | detail::printBumpPtrAllocatorStats(Slabs.size(), BytesAllocated, |
293 | getTotalMemory()); |
294 | } |
295 | |
296 | private: |
297 | |
298 | |
299 | |
300 | char *CurPtr = nullptr; |
301 | |
302 | |
303 | char *End = nullptr; |
304 | |
305 | |
306 | SmallVector<void *, 4> Slabs; |
307 | |
308 | |
309 | SmallVector<std::pair<void *, size_t>, 0> CustomSizedSlabs; |
310 | |
311 | |
312 | |
313 | |
314 | size_t BytesAllocated = 0; |
315 | |
316 | |
317 | |
318 | size_t RedZoneSize = 1; |
319 | |
320 | static size_t computeSlabSize(unsigned SlabIdx) { |
321 | |
322 | |
323 | |
324 | |
325 | return SlabSize * |
326 | ((size_t)1 << std::min<size_t>(30, SlabIdx / GrowthDelay)); |
327 | } |
328 | |
329 | |
330 | |
331 | void StartNewSlab() { |
332 | size_t AllocatedSlabSize = computeSlabSize(Slabs.size()); |
333 | |
334 | void *NewSlab = |
335 | AllocatorT::Allocate(AllocatedSlabSize, alignof(std::max_align_t)); |
336 | |
337 | |
338 | __asan_poison_memory_region(NewSlab, AllocatedSlabSize); |
339 | |
340 | Slabs.push_back(NewSlab); |
341 | CurPtr = (char *)(NewSlab); |
342 | End = ((char *)NewSlab) + AllocatedSlabSize; |
343 | } |
344 | |
345 | |
346 | void DeallocateSlabs(SmallVectorImpl<void *>::iterator I, |
347 | SmallVectorImpl<void *>::iterator E) { |
348 | for (; I != E; ++I) { |
349 | size_t AllocatedSlabSize = |
350 | computeSlabSize(std::distance(Slabs.begin(), I)); |
351 | AllocatorT::Deallocate(*I, AllocatedSlabSize, alignof(std::max_align_t)); |
352 | } |
353 | } |
354 | |
355 | |
356 | void DeallocateCustomSizedSlabs() { |
357 | for (auto &PtrAndSize : CustomSizedSlabs) { |
358 | void *Ptr = PtrAndSize.first; |
359 | size_t Size = PtrAndSize.second; |
360 | AllocatorT::Deallocate(Ptr, Size, alignof(std::max_align_t)); |
361 | } |
362 | } |
363 | |
364 | template <typename T> friend class SpecificBumpPtrAllocator; |
365 | }; |
366 | |
367 | |
368 | |
369 | typedef BumpPtrAllocatorImpl<> BumpPtrAllocator; |
370 | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | template <typename T> class SpecificBumpPtrAllocator { |
377 | BumpPtrAllocator Allocator; |
378 | |
379 | public: |
380 | SpecificBumpPtrAllocator() { |
381 | |
382 | |
383 | Allocator.setRedZoneSize(0); |
384 | } |
385 | SpecificBumpPtrAllocator(SpecificBumpPtrAllocator &&Old) |
386 | : Allocator(std::move(Old.Allocator)) {} |
387 | ~SpecificBumpPtrAllocator() { DestroyAll(); } |
388 | |
389 | SpecificBumpPtrAllocator &operator=(SpecificBumpPtrAllocator &&RHS) { |
390 | Allocator = std::move(RHS.Allocator); |
391 | return *this; |
392 | } |
393 | |
394 | |
395 | |
396 | |
397 | void DestroyAll() { |
398 | auto DestroyElements = [](char *Begin, char *End) { |
399 | assert(Begin == (char *)alignAddr(Begin, Align::Of<T>())); |
400 | for (char *Ptr = Begin; Ptr + sizeof(T) <= End; Ptr += sizeof(T)) |
401 | reinterpret_cast<T *>(Ptr)->~T(); |
402 | }; |
403 | |
404 | for (auto I = Allocator.Slabs.begin(), E = Allocator.Slabs.end(); I != E; |
405 | ++I) { |
406 | size_t AllocatedSlabSize = BumpPtrAllocator::computeSlabSize( |
407 | std::distance(Allocator.Slabs.begin(), I)); |
408 | char *Begin = (char *)alignAddr(*I, Align::Of<T>()); |
409 | char *End = *I == Allocator.Slabs.back() ? Allocator.CurPtr |
410 | : (char *)*I + AllocatedSlabSize; |
411 | |
412 | DestroyElements(Begin, End); |
413 | } |
414 | |
415 | for (auto &PtrAndSize : Allocator.CustomSizedSlabs) { |
416 | void *Ptr = PtrAndSize.first; |
417 | size_t Size = PtrAndSize.second; |
418 | DestroyElements((char *)alignAddr(Ptr, Align::Of<T>()), |
419 | (char *)Ptr + Size); |
420 | } |
421 | |
422 | Allocator.Reset(); |
423 | } |
424 | |
425 | |
426 | T *Allocate(size_t num = 1) { return Allocator.Allocate<T>(num); } |
427 | }; |
428 | |
429 | } |
430 | |
431 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
432 | size_t GrowthDelay> |
433 | void * |
434 | operator new(size_t Size, |
435 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, SizeThreshold, |
436 | GrowthDelay> &Allocator) { |
437 | return Allocator.Allocate(Size, std::min((size_t)llvm::NextPowerOf2(Size), |
| 19 | | Calling 'BumpPtrAllocatorImpl::Allocate' | |
|
438 | alignof(std::max_align_t))); |
439 | } |
440 | |
441 | template <typename AllocatorT, size_t SlabSize, size_t SizeThreshold, |
442 | size_t GrowthDelay> |
443 | void operator delete(void *, |
444 | llvm::BumpPtrAllocatorImpl<AllocatorT, SlabSize, |
445 | SizeThreshold, GrowthDelay> &) { |
446 | } |
447 | |
448 | #endif // LLVM_SUPPORT_ALLOCATOR_H |