-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
IncrementalParser.cpp
1034 lines (888 loc) · 39.3 KB
/
IncrementalParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// CLING - the C++ LLVM-based InterpreterG :)
// author: Axel Naumann <axel@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "IncrementalParser.h"
#include "ASTTransformer.h"
#include "AutoSynthesizer.h"
#include "CheckEmptyTransactionTransformer.h"
#include "ClingPragmas.h"
#include "DeclCollector.h"
#include "DeclExtractor.h"
#include "DefinitionShadower.h"
#include "DeviceKernelInliner.h"
#include "DynamicLookup.h"
#include "NullDerefProtectionTransformer.h"
#include "TransactionPool.h"
#include "ValueExtractionSynthesizer.h"
#include "ValuePrinterSynthesizer.h"
#include "cling/Interpreter/CIFactory.h"
#include "cling/Interpreter/Interpreter.h"
#include "cling/Interpreter/InterpreterCallbacks.h"
#include "cling/Interpreter/Transaction.h"
#include "cling/Utils/Diagnostics.h"
#include "cling/Utils/Output.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclGroup.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/FileManager.h"
#include "clang/CodeGen/ModuleBuilder.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Parse/Parser.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/SemaDiagnostic.h"
#include "clang/Serialization/ASTWriter.h"
#include "clang/Serialization/ASTReader.h"
#include "llvm/Support/Path.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/MemoryBuffer.h"
#include <stdio.h>
using namespace clang;
namespace {
///\brief Check the compile-time C++ ABI version vs the run-time ABI version,
/// a mismatch could cause havoc. Reports if ABI versions differ.
static bool CheckABICompatibility(cling::Interpreter& Interp) {
#if defined(__GLIBCXX__)
#define CLING_CXXABI_VERS std::to_string(__GLIBCXX__)
const char* CLING_CXXABI_NAME = "__GLIBCXX__";
static constexpr bool CLING_CXXABI_BACKWARDCOMP = true;
#elif defined(_LIBCPP_VERSION)
#define CLING_CXXABI_VERS std::to_string(_LIBCPP_ABI_VERSION)
const char* CLING_CXXABI_NAME = "_LIBCPP_ABI_VERSION";
static constexpr bool CLING_CXXABI_BACKWARDCOMP = false;
#elif defined(_CRT_MSVCP_CURRENT)
#define CLING_CXXABI_VERS _CRT_MSVCP_CURRENT
const char* CLING_CXXABI_NAME = "_CRT_MSVCP_CURRENT";
static constexpr bool CLING_CXXABI_BACKWARDCOMP = false;
#else
#error "Unknown platform for ABI check";
#endif
const std::string CurABI = Interp.getMacroValue(CLING_CXXABI_NAME);
if (CurABI == CLING_CXXABI_VERS)
return true;
if (CurABI.empty()) {
cling::errs() <<
"Warning in cling::IncrementalParser::CheckABICompatibility():\n"
" Failed to extract C++ standard library version.\n";
}
if (CLING_CXXABI_BACKWARDCOMP && CurABI < CLING_CXXABI_VERS) {
// Backward compatible ABIs allow us to interpret old headers
// against a newer stdlib.so.
return true;
}
cling::errs() <<
"Warning in cling::IncrementalParser::CheckABICompatibility():\n"
" Possible C++ standard library mismatch, compiled with "
<< CLING_CXXABI_NAME << " '" << CLING_CXXABI_VERS << "'\n"
" Extraction of runtime standard library version was: '"
<< CurABI << "'\n";
return false;
}
/// \brief Overrides the current DiagnosticConsumer to supress many warnings
/// issued as a result of incremental compilation (see `HandleDiagnostic()`).
///
/// Diagnostics passing the filter are, by default, forwarded to the previous
/// DiagnosticConsumer instance. A different consumer may be specified via
/// `setTargetConsumer()`.
/// In that case, given that internal state might be updated as part of
/// `{Begin,End}SourceFile` (e.g. in TextDiagnosticPrinter), calls to such
/// functions will be forwarded to both, the user-specified and the original
/// consumer; however `HandleDiagnostic()` calls shall only be seen by the
/// former.
///
/// On destruction, the original (i.e. overridden consumer) is restored.
///
class FilteringDiagConsumer : public cling::utils::DiagnosticsOverride {
std::stack<bool> m_IgnorePromptDiags;
llvm::PointerIntPair<DiagnosticConsumer*, 1, bool /*Own*/> m_Target{};
void SyncDiagCountWithTarget() {
NumWarnings = m_PrevClient.getNumWarnings();
NumErrors = m_PrevClient.getNumErrors();
}
void BeginSourceFile(const LangOptions &LangOpts,
const Preprocessor *PP=nullptr) override {
m_PrevClient.BeginSourceFile(LangOpts, PP);
if (auto C = m_Target.getPointer())
C->BeginSourceFile(LangOpts, PP);
}
void EndSourceFile() override {
m_PrevClient.EndSourceFile();
if (auto C = m_Target.getPointer())
C->EndSourceFile();
SyncDiagCountWithTarget();
}
void finish() override {
m_PrevClient.finish();
if (auto C = m_Target.getPointer())
C->finish();
SyncDiagCountWithTarget();
}
void clear() override {
m_PrevClient.clear();
if (auto C = m_Target.getPointer())
C->clear();
SyncDiagCountWithTarget();
}
bool IncludeInDiagnosticCounts() const override {
return m_PrevClient.IncludeInDiagnosticCounts();
}
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const Diagnostic &Info) override {
if (Ignoring()) {
if (Info.getID() == diag::warn_unused_expr
|| Info.getID() == diag::warn_unused_result
|| Info.getID() == diag::warn_unused_call
|| Info.getID() == diag::warn_unused_comparison)
return; // ignore!
if (Info.getID() == diag::warn_falloff_nonvoid_function) {
DiagLevel = DiagnosticsEngine::Error;
}
if (Info.getID() == diag::ext_return_has_expr) {
// An error that we need to suppress.
auto Diags = const_cast<DiagnosticsEngine*>(Info.getDiags());
assert(Diags->hasErrorOccurred() && "Expected ErrorOccurred");
if (m_PrevClient.getNumErrors() == 0) { // first error
Diags->Reset(true /*soft - only counts, not mappings*/);
} // else we had other errors, too.
return; // ignore!
}
}
// In principle, for simplicity, we preserve the old behavior of
// delivering diagnostics to just one consumer (that is why we don't emit
// to both), but we allow the "sink" to be changed.
// Note, however, that consumers might update their internal state in
// calls to, e.g. `BeginSourceFile()` or `EndSourceFile()` (actually,
// `TextDiagnosticPrinter` is an example of this), so in order to be able
// to restore the original consumer, we need to keep forwarding these
// calls also to `m_PrevClient` (see above).
if (auto C = m_Target.getPointer())
C->HandleDiagnostic(DiagLevel, Info);
else
m_PrevClient.HandleDiagnostic(DiagLevel, Info);
SyncDiagCountWithTarget();
}
bool Ignoring() const {
return !m_IgnorePromptDiags.empty() && m_IgnorePromptDiags.top();
}
public:
FilteringDiagConsumer(DiagnosticsEngine& Diags, bool Own)
: DiagnosticsOverride(Diags, Own) {}
virtual ~FilteringDiagConsumer() { setTargetConsumer(nullptr); }
/// \brief Sets the DiagnosticConsumer that sees `HandleDiagnostic()` calls.
/// \param[in] Consumer - The target DiagnosticConsumer, or `nullptr` to
/// revert to original client.
/// \param[in] Own - Whether we own the pointee
///
void setTargetConsumer(DiagnosticConsumer* Consumer, bool Own = false) {
if (m_Target.getInt())
if (auto C = m_Target.getPointer())
delete C;
m_Target.setPointer(Consumer);
m_Target.setInt(Own);
}
DiagnosticConsumer* getTargetConsumer() const
{ return m_Target.getPointer(); }
struct RAAI {
FilteringDiagConsumer& m_Client;
RAAI(DiagnosticConsumer& F, bool Ignore) :
m_Client(static_cast<FilteringDiagConsumer&>(F)) {
m_Client.m_IgnorePromptDiags.push(Ignore);
}
~RAAI() { m_Client.m_IgnorePromptDiags.pop(); }
};
};
} // unnamed namespace
static void HandlePlugins(CompilerInstance& CI,
std::vector<std::unique_ptr<ASTConsumer>>& Consumers) {
// Copied from Frontend/FrontendAction.cpp.
// FIXME: Remove when we switch to a tools-based cling driver.
// If the FrontendPluginRegistry has plugins before loading any shared library
// this means we have linked our plugins. This is useful when cling runs in
// embedded mode (in a shared library). This is the only feasible way to have
// plugins if cling is in a single shared library which is dlopen-ed with
// RTLD_LOCAL. In that situation plugins can still find the cling, clang and
// llvm symbols opened with local visibility.
if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end()) {
for (const std::string& Path : CI.getFrontendOpts().Plugins) {
std::string Err;
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Err))
CI.getDiagnostics().Report(clang::diag::err_fe_unable_to_load_plugin)
<< Path << Err;
}
// If we are not statically linked, we should register the pragmas ourselves
// because the dlopen happens after creating the clang::Preprocessor which
// calls RegisterBuiltinPragmas.
// FIXME: This can be avoided by refactoring our routine and moving it to
// the CIFactory. This requires an abstraction which allows us to
// conditionally create MultiplexingConsumers.
// Copied from Lex/Pragma.cpp
// Pragmas added by plugins
for (PragmaHandlerRegistry::iterator it = PragmaHandlerRegistry::begin(),
ie = PragmaHandlerRegistry::end(); it != ie; ++it)
CI.getPreprocessor().AddPragmaHandler(it->instantiate().release());
}
for (auto it = clang::FrontendPluginRegistry::begin(),
ie = clang::FrontendPluginRegistry::end();
it != ie; ++it) {
std::unique_ptr<clang::PluginASTAction> P(it->instantiate());
PluginASTAction::ActionType PluginActionType = P->getActionType();
assert(PluginActionType != clang::PluginASTAction::ReplaceAction);
if (P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName().str()])) {
std::unique_ptr<ASTConsumer> PluginConsumer
= P->CreateASTConsumer(CI, /*InputFile*/ "");
if (PluginActionType == clang::PluginASTAction::AddBeforeMainAction)
Consumers.insert(Consumers.begin(), std::move(PluginConsumer));
else
Consumers.push_back(std::move(PluginConsumer));
}
}
}
namespace cling {
IncrementalParser::IncrementalParser(Interpreter* interp, const char* llvmdir,
const ModuleFileExtensions& moduleExtensions)
: m_Interpreter(interp) {
std::unique_ptr<cling::DeclCollector> consumer;
consumer.reset(m_Consumer = new cling::DeclCollector());
m_CI.reset(CIFactory::createCI("\n", interp->getOptions(), llvmdir,
std::move(consumer), moduleExtensions));
if (!m_CI) {
cling::errs() << "Compiler instance could not be created.\n";
return;
}
// Is the CompilerInstance being used to generate output only?
if (m_Interpreter->getOptions().CompilerOpts.HasOutput)
return;
if (!m_Consumer) {
cling::errs() << "No AST consumer available.\n";
return;
}
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
HandlePlugins(*m_CI, Consumers);
std::unique_ptr<ASTConsumer> WrappedConsumer;
DiagnosticsEngine& Diag = m_CI->getDiagnostics();
if (m_CI->getFrontendOpts().ProgramAction != frontend::ParseSyntaxOnly) {
auto CG
= std::unique_ptr<clang::CodeGenerator>(CreateLLVMCodeGen(Diag,
makeModuleName(),
m_CI->getHeaderSearchOpts(),
m_CI->getPreprocessorOpts(),
m_CI->getCodeGenOpts(),
*m_Interpreter->getLLVMContext())
);
m_CodeGen = CG.get();
assert(m_CodeGen);
if (!Consumers.empty()) {
Consumers.push_back(std::move(CG));
WrappedConsumer.reset(new MultiplexConsumer(std::move(Consumers)));
}
else
WrappedConsumer = std::move(CG);
}
// Initialize the DeclCollector and add callbacks keeping track of macros.
m_Consumer->Setup(this, std::move(WrappedConsumer), m_CI->getPreprocessor());
m_DiagConsumer.reset(new FilteringDiagConsumer(Diag, false));
initializeVirtualFile();
}
bool
IncrementalParser::Initialize(llvm::SmallVectorImpl<ParseResultTransaction>&
result, bool isChildInterpreter) {
m_TransactionPool.reset(new TransactionPool);
if (hasCodeGenerator())
getCodeGenerator()->Initialize(getCI()->getASTContext());
CompilationOptions CO = m_Interpreter->makeDefaultCompilationOpts();
Transaction* CurT = beginTransaction(CO);
Preprocessor& PP = m_CI->getPreprocessor();
DiagnosticsEngine& Diags = m_CI->getSema().getDiagnostics();
// Pull in PCH.
const std::string& PCHFileName
= m_CI->getInvocation().getPreprocessorOpts().ImplicitPCHInclude;
if (!PCHFileName.empty()) {
Transaction* PchT = beginTransaction(CO);
DiagnosticErrorTrap Trap(Diags);
m_CI->createPCHExternalASTSource(PCHFileName,
DisableValidationForModuleKind::All,
true /*AllowPCHWithCompilerErrors*/,
nullptr /*DeserializationListener*/,
true /*OwnsDeserializationListener*/);
result.push_back(endTransaction(PchT));
if (Trap.hasErrorOccurred()) {
result.push_back(endTransaction(CurT));
return false;
}
}
addClingPragmas(*m_Interpreter);
// Must happen after attaching the PCH, else PCH elements will end up
// being lexed.
PP.EnterMainSourceFile();
Sema* TheSema = &m_CI->getSema();
m_Parser.reset(new Parser(PP, *TheSema, false /*skipFuncBodies*/));
// Initialize the parser after PP has entered the main source file.
m_Parser->Initialize();
ExternalASTSource *External = TheSema->getASTContext().getExternalSource();
if (External)
External->StartTranslationUnit(m_Consumer);
// Start parsing the "main file" to warm up lexing (enter caching lex mode
// for ParseInternal()'s call EnterSourceFile() to make sense.
while (!m_Parser->ParseTopLevelDecl()) {}
// If I belong to the parent Interpreter, am using C++, and -noruntime
// wasn't given on command line, then #include <new> and check ABI
if (!isChildInterpreter && m_CI->getLangOpts().CPlusPlus &&
!m_Interpreter->getOptions().NoRuntime) {
// <new> is needed by the ValuePrinter so it's a good thing to include it.
// We need to include it to determine the version number of the standard
// library implementation.
ParseInternal("#include <new>");
// That's really C++ ABI compatibility. C has other problems ;-)
CheckABICompatibility(*m_Interpreter);
}
// DO NOT commit the transactions here: static initialization in these
// transactions requires gCling through local_cxa_atexit(), but that has not
// been defined yet!
ParseResultTransaction PRT = endTransaction(CurT);
result.push_back(PRT);
return true;
}
bool IncrementalParser::isValid(bool initialized) const {
return m_CI && m_CI->hasFileManager() && m_Consumer
&& !m_VirtualFileID.isInvalid()
&& (!initialized || (m_TransactionPool && m_Parser));
}
namespace {
template <class T>
struct Reversed {
const T &m_orig;
auto begin() -> decltype(m_orig.rbegin()) { return m_orig.rbegin(); }
auto end() -> decltype (m_orig.rend()) { return m_orig.rend(); }
};
template <class T>
Reversed<T> reverse(const T& orig) { return {orig}; }
}
const Transaction* IncrementalParser::getLastWrapperTransaction() const {
if (auto *T = getCurrentTransaction())
if (T->getWrapperFD())
return T;
for (auto T: reverse(m_Transactions))
if (T->getWrapperFD())
return T;
return nullptr;
}
const Transaction* IncrementalParser::getCurrentTransaction() const {
return m_Consumer->getTransaction();
}
void IncrementalParser::setDiagnosticConsumer(DiagnosticConsumer* Consumer,
bool Own) {
static_cast<
FilteringDiagConsumer&>(*m_DiagConsumer).setTargetConsumer(Consumer, Own);
}
DiagnosticConsumer* IncrementalParser::getDiagnosticConsumer() const {
return static_cast<
FilteringDiagConsumer&>(*m_DiagConsumer).getTargetConsumer();
}
SourceLocation IncrementalParser::getNextAvailableUniqueSourceLoc() {
const SourceManager& SM = getCI()->getSourceManager();
SourceLocation Result = SM.getLocForStartOfFile(m_VirtualFileID);
return Result.getLocWithOffset(m_VirtualFileLocOffset++);
}
IncrementalParser::~IncrementalParser() {
Transaction* T = const_cast<Transaction*>(getFirstTransaction());
while (T) {
assert((T->getState() == Transaction::kCommitted
|| T->getState() == Transaction::kRolledBackWithErrors
|| T->getState() == Transaction::kNumStates // reset from the pool
|| T->getState() == Transaction::kRolledBack)
&& "Not committed?");
const Transaction* nextT = T->getNext();
m_TransactionPool->releaseTransaction(T, false);
T = const_cast<Transaction*>(nextT);
}
}
void IncrementalParser::addTransaction(Transaction* T) {
if (!T->isNestedTransaction() && T != getLastTransaction()) {
if (getLastTransaction())
m_Transactions.back()->setNext(T);
m_Transactions.push_back(T);
}
}
Transaction* IncrementalParser::beginTransaction(const CompilationOptions&
Opts) {
Transaction* OldCurT = m_Consumer->getTransaction();
Transaction* NewCurT = m_TransactionPool->takeTransaction(m_CI->getSema());
NewCurT->setCompilationOpts(Opts);
// If we are in the middle of transaction and we see another begin
// transaction - it must be nested transaction.
if (OldCurT && OldCurT != NewCurT
&& (OldCurT->getState() == Transaction::kCollecting
|| OldCurT->getState() == Transaction::kCompleted)) {
OldCurT->addNestedTransaction(NewCurT); // takes the ownership
}
m_Consumer->setTransaction(NewCurT);
return NewCurT;
}
IncrementalParser::ParseResultTransaction
IncrementalParser::endTransaction(Transaction* T) {
assert(T && "Null transaction!?");
assert(T->getState() == Transaction::kCollecting);
#ifndef NDEBUG
if (T->hasNestedTransactions()) {
for(Transaction::const_nested_iterator I = T->nested_begin(),
E = T->nested_end(); I != E; ++I)
assert((*I)->isCompleted() && "Nested transaction not completed!?");
}
#endif
T->setState(Transaction::kCompleted);
DiagnosticsEngine& Diag = getCI()->getSema().getDiagnostics();
//TODO: Make the enum orable.
EParseResult ParseResult = kSuccess;
assert((Diag.hasFatalErrorOccurred() ? Diag.hasErrorOccurred() : true)
&& "Diag.hasFatalErrorOccurred without Diag.hasErrorOccurred !");
if (Diag.hasErrorOccurred() || T->getIssuedDiags() == Transaction::kErrors) {
T->setIssuedDiags(Transaction::kErrors);
ParseResult = kFailed;
} else if (Diag.getNumWarnings() > 0) {
T->setIssuedDiags(Transaction::kWarnings);
ParseResult = kSuccessWithWarnings;
}
// Empty transaction, send it back to the pool.
if (T->empty()) {
assert((!m_Consumer->getTransaction()
|| (m_Consumer->getTransaction() == T))
&& "Cannot release different T");
// If a nested transaction the active one should be its parent
// from now on. FIXME: Merge conditional with commitTransaction
if (T->isNestedTransaction())
m_Consumer->setTransaction(T->getParent());
else
m_Consumer->setTransaction((Transaction*)0);
m_TransactionPool->releaseTransaction(T);
return ParseResultTransaction(nullptr, ParseResult);
}
addTransaction(T);
return ParseResultTransaction(T, ParseResult);
}
std::string IncrementalParser::makeModuleName() {
return std::string("cling-module-") + std::to_string(m_ModuleNo++);
}
llvm::Module* IncrementalParser::StartModule() {
return getCodeGenerator()->StartModule(makeModuleName(),
*m_Interpreter->getLLVMContext(),
getCI()->getCodeGenOpts());
}
void IncrementalParser::commitTransaction(ParseResultTransaction& PRT,
bool ClearDiagClient) {
Transaction* T = PRT.getPointer();
if (!T) {
if (PRT.getInt() != kSuccess) {
// Nothing has been emitted to Codegen, reset the Diags.
DiagnosticsEngine& Diags = getCI()->getSema().getDiagnostics();
Diags.Reset(/*soft=*/true);
if (ClearDiagClient)
Diags.getClient()->clear();
}
return;
}
assert(T->isCompleted() && "Transaction not ended!?");
assert(T->getState() != Transaction::kCommitted
&& "Committing an already committed transaction.");
assert((T->getIssuedDiags() == Transaction::kErrors || !T->empty())
&& "Valid Transactions must not be empty;");
// If committing a nested transaction the active one should be its parent
// from now on.
if (T->isNestedTransaction())
m_Consumer->setTransaction(T->getParent());
// Check for errors...
if (T->getIssuedDiags() == Transaction::kErrors) {
// Make module visible to TransactionUnloader.
bool MustStartNewModule = false;
if (!T->isNestedTransaction() && hasCodeGenerator()) {
MustStartNewModule = true;
std::unique_ptr<llvm::Module> M(getCodeGenerator()->ReleaseModule());
if (M) {
T->setModule(std::move(M));
}
}
// Module has been released from Codegen, reset the Diags now.
DiagnosticsEngine& Diags = getCI()->getSema().getDiagnostics();
Diags.Reset(/*soft=*/true);
if (ClearDiagClient)
Diags.getClient()->clear();
PRT.setPointer(nullptr);
PRT.setInt(kFailed);
m_Interpreter->unload(*T);
// Create a new module if necessary.
if (MustStartNewModule)
StartModule();
return;
}
if (T->hasNestedTransactions()) {
Transaction* TopmostParent = T->getTopmostParent();
EParseResult PR = kSuccess;
if (TopmostParent->getIssuedDiags() == Transaction::kErrors)
PR = kFailed;
else if (TopmostParent->getIssuedDiags() == Transaction::kWarnings)
PR = kSuccessWithWarnings;
for (Transaction::const_nested_iterator I = T->nested_begin(),
E = T->nested_end(); I != E; ++I)
if ((*I)->getState() != Transaction::kCommitted) {
ParseResultTransaction PRT(*I, PR);
commitTransaction(PRT);
}
}
// If there was an error coming from the transformers.
if (T->getIssuedDiags() == Transaction::kErrors) {
m_Interpreter->unload(*T);
return;
}
// Here we expect a template instantiation. We need to open the transaction
// that we are currently work with.
{
Transaction* prevConsumerT = m_Consumer->getTransaction();
m_Consumer->setTransaction(T);
Transaction* nestedT = beginTransaction(T->getCompilationOpts());
// Process used vtables and generate implicit bodies.
getCI()->getSema().DefineUsedVTables();
// Pull all template instantiations in that came from the consumers.
getCI()->getSema().PerformPendingInstantiations();
#ifdef _WIN32
// Microsoft-specific:
// Late parsed templates can leave unswallowed "macro"-like tokens.
// They will seriously confuse the Parser when entering the next
// source file. So lex until we are EOF.
Token Tok;
Tok.setKind(tok::eof);
do {
getCI()->getSema().getPreprocessor().Lex(Tok);
} while (Tok.isNot(tok::eof));
#endif
ParseResultTransaction nestedPRT = endTransaction(nestedT);
commitTransaction(nestedPRT);
m_Consumer->setTransaction(prevConsumerT);
}
m_Consumer->HandleTranslationUnit(getCI()->getASTContext());
// The static initializers might run anything and can thus cause more
// decls that need to end up in a transaction. But this one is done
// with CodeGen...
if (T->getCompilationOpts().CodeGeneration && hasCodeGenerator()) {
Transaction* prevConsumerT = m_Consumer->getTransaction();
m_Consumer->setTransaction(T);
codeGenTransaction(T);
T->setState(Transaction::kCommitted);
if (!T->getParent()) {
if (m_Interpreter->executeTransaction(*T)
>= Interpreter::kExeFirstError) {
// Roll back on error in initializers.
// T maybe pointing to freed memory after this call:
// Interpreter::unload
// IncrementalParser::deregisterTransaction
// TransactionPool::releaseTransaction
m_Interpreter->unload(*T);
return;
}
}
m_Consumer->setTransaction(prevConsumerT);
}
T->setState(Transaction::kCommitted);
{
Transaction* prevConsumerT = m_Consumer->getTransaction();
if (InterpreterCallbacks* callbacks = m_Interpreter->getCallbacks())
callbacks->TransactionCommitted(*T);
m_Consumer->setTransaction(prevConsumerT);
}
}
void IncrementalParser::emitTransaction(Transaction* T) {
for (auto DI = T->decls_begin(), DE = T->decls_end(); DI != DE; ++DI)
m_Consumer->HandleTopLevelDecl(DI->m_DGR);
}
void IncrementalParser::codeGenTransaction(Transaction* T) {
// codegen the transaction
assert(T->getCompilationOpts().CodeGeneration && "CodeGen turned off");
assert(T->getState() == Transaction::kCompleted && "Must be completed");
assert(hasCodeGenerator() && "No CodeGen");
// Could trigger derserialization of decls.
Transaction* deserT = beginTransaction(CompilationOptions());
// Commit this transaction first - T might need symbols from it, so
// trigger emission of weak symbols by providing use.
ParseResultTransaction PRT = endTransaction(deserT);
commitTransaction(PRT);
deserT = PRT.getPointer();
// This llvm::Module is done; finalize it and pass it to the execution
// engine.
if (!T->isNestedTransaction() && hasCodeGenerator()) {
if (InterpreterCallbacks* callbacks = m_Interpreter->getCallbacks())
callbacks->TransactionCodeGenStarted(*T);
// Update CodeGen to current optimization level, which might be different
// from what it had when constructed.
auto &CGOpts = m_CI->getCodeGenOpts();
CGOpts.OptimizationLevel = T->getCompilationOpts().OptLevel;
CGOpts.setInlining((CGOpts.OptimizationLevel == 0)
? CodeGenOptions::OnlyAlwaysInlining
: CodeGenOptions::NormalInlining);
// The initializers are emitted to the symbol "_GLOBAL__sub_I_" + filename.
// Make that unique!
deserT = beginTransaction(CompilationOptions());
// Reset the module builder to clean up global initializers, c'tors, d'tors
getCodeGenerator()->HandleTranslationUnit(getCI()->getASTContext());
auto PRT = endTransaction(deserT);
commitTransaction(PRT);
deserT = PRT.getPointer();
std::unique_ptr<llvm::Module> M(getCodeGenerator()->ReleaseModule());
if (M)
T->setModule(std::move(M));
if (T->getIssuedDiags() != Transaction::kNone) {
// Module has been released from Codegen, reset the Diags now.
DiagnosticsEngine& Diags = getCI()->getSema().getDiagnostics();
Diags.Reset(/*soft=*/true);
Diags.getClient()->clear();
}
if (InterpreterCallbacks* callbacks = m_Interpreter->getCallbacks())
callbacks->TransactionCodeGenFinished(*T);
// Create a new module.
StartModule();
}
}
void IncrementalParser::deregisterTransaction(Transaction& T) {
if (&T == m_Consumer->getTransaction())
m_Consumer->setTransaction(T.getParent());
if (Transaction* Parent = T.getParent()) {
Parent->removeNestedTransaction(&T);
T.setParent(nullptr);
} else {
if (&T == m_Transactions.back()) {
// Remove from the queue
m_Transactions.pop_back();
if (!m_Transactions.empty())
m_Transactions.back()->setNext(nullptr);
} else {
// If T is not the last transaction it must not be a previous
// transaction either, but a "disconnected" one, i.e. one that
// was not yet committed.
assert(std::find(m_Transactions.begin(), m_Transactions.end(), &T)
== m_Transactions.end() && "Out of order transaction removal");
}
}
m_TransactionPool->releaseTransaction(&T);
}
std::vector<const Transaction*> IncrementalParser::getAllTransactions() {
std::vector<const Transaction*> result(m_Transactions.size());
const cling::Transaction* T = getFirstTransaction();
while (T) {
result.push_back(T);
T = T->getNext();
}
return result;
}
// Each input line is contained in separate memory buffer. The SourceManager
// assigns sort-of invalid FileID for each buffer, i.e there is no FileEntry
// for the MemoryBuffer's FileID. That in turn is problem because invalid
// SourceLocations are given to the diagnostics. Thus the diagnostics cannot
// order the overloads, for example
//
// Our work-around is creating a virtual file, which doesn't exist on the disk
// with enormous size (no allocation is done). That file has valid FileEntry
// and so on... We use it for generating valid SourceLocations with valid
// offsets so that it doesn't cause any troubles to the diagnostics.
//
// +---------------------+
// | Main memory buffer |
// +---------------------+
// | Virtual file SLoc |
// | address space |<-----------------+
// | ... |<------------+ |
// | ... | | |
// | ... |<----+ | |
// | ... | | | |
// +~~~~~~~~~~~~~~~~~~~~~+ | | |
// | input_line_1 | ....+.......+..--+
// +---------------------+ | |
// | input_line_2 | ....+.....--+
// +---------------------+ |
// | ... | |
// +---------------------+ |
// | input_line_N | ..--+
// +---------------------+
//
void IncrementalParser::initializeVirtualFile() {
SourceManager& SM = getCI()->getSourceManager();
m_VirtualFileID = SM.getMainFileID();
if (m_VirtualFileID.isInvalid())
cling::errs() << "VirtualFileID could not be created.\n";
}
IncrementalParser::ParseResultTransaction
IncrementalParser::Compile(llvm::StringRef input,
const CompilationOptions& Opts) {
Transaction* CurT = beginTransaction(Opts);
EParseResult ParseRes = ParseInternal(input);
if (ParseRes == kSuccessWithWarnings)
CurT->setIssuedDiags(Transaction::kWarnings);
else if (ParseRes == kFailed)
CurT->setIssuedDiags(Transaction::kErrors);
ParseResultTransaction PRT = endTransaction(CurT);
commitTransaction(PRT);
return PRT;
}
// Add the input to the memory buffer, parse it, and add it to the AST.
IncrementalParser::EParseResult
IncrementalParser::ParseInternal(llvm::StringRef input) {
if (input.empty()) return IncrementalParser::kSuccess;
Sema& S = getCI()->getSema();
const CompilationOptions& CO
= m_Consumer->getTransaction()->getCompilationOpts();
// Recover resources if we crash before exiting this method.
llvm::CrashRecoveryContextCleanupRegistrar<Sema> CleanupSema(&S);
Preprocessor& PP = m_CI->getPreprocessor();
if (!PP.getCurrentLexer()) {
PP.EnterSourceFile(m_CI->getSourceManager().getMainFileID(),
nullptr, SourceLocation());
}
assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode!?");
PP.enableIncrementalProcessing();
smallstream source_name;
source_name << "input_line_" << (m_MemoryBuffers.size() + 1);
// Create an uninitialized memory buffer, copy code in and append "\n"
size_t InputSize = input.size(); // don't include trailing 0
// MemBuffer size should *not* include terminating zero
std::unique_ptr<llvm::WritableMemoryBuffer>
MB(llvm::WritableMemoryBuffer::getNewUninitMemBuffer(InputSize + 1,
source_name.str()));
char* MBStart = MB->getBufferStart();
memcpy(MBStart, input.data(), InputSize);
MBStart[InputSize] = '\n';
SourceManager& SM = getCI()->getSourceManager();
// Create SourceLocation, which will allow clang to order the overload
// candidates for example
SourceLocation NewLoc = getNextAvailableUniqueSourceLoc();
llvm::MemoryBuffer* MBNonOwn = MB.get();
// Create FileID for the current buffer.
FileID FID;
// Create FileEntry and FileID for the current buffer.
// Enabling the completion point only works on FileEntries.
const clang::FileEntry* FE
= SM.getFileManager().getVirtualFile(source_name.str(), InputSize,
0 /* mod time*/);
SM.overrideFileContents(FE, std::move(MB));
FID = SM.createFileID(FE, NewLoc, SrcMgr::C_User);
if (CO.CodeCompletionOffset != -1) {
// The completion point is set one a 1-based line/column numbering.
// It relies on the implementation to account for the wrapper extra line.
PP.SetCodeCompletionPoint(FE, 1/* start point 1-based line*/,
CO.CodeCompletionOffset+1/* 1-based column*/);
}
m_MemoryBuffers.push_back(std::make_pair(MBNonOwn, FID));
// NewLoc only used for diags.
PP.EnterSourceFile(FID, /*DirLookup*/nullptr, NewLoc);
m_Consumer->getTransaction()->setBufferFID(FID);
DiagnosticsEngine& Diags = getCI()->getDiagnostics();
FilteringDiagConsumer::RAAI RAAITmp(*m_DiagConsumer, CO.IgnorePromptDiags);
DiagnosticErrorTrap Trap(Diags);
// FIXME: SavePendingInstantiationsRAII should be obsolvete as
// GlobalEagerInstantiationScope and LocalEagerInstantiationScope do the
// same thing.
Sema::SavePendingInstantiationsRAII SavedPendingInstantiations(S);
Sema::GlobalEagerInstantiationScope GlobalInstantiations(S, /*Enabled=*/true);
Sema::LocalEagerInstantiationScope LocalInstantiations(S);
Parser::DeclGroupPtrTy ADecl;
while (!m_Parser->ParseTopLevelDecl(ADecl)) {
// If we got a null return and something *was* parsed, ignore it. This
// is due to a top-level semicolon, an action override, or a parse error
// skipping something.
if (Trap.hasErrorOccurred())
m_Consumer->getTransaction()->setIssuedDiags(Transaction::kErrors);
if (ADecl)
m_Consumer->HandleTopLevelDecl(ADecl.get());
};
// If never entered the while block, there's a chance an error occured
if (Trap.hasErrorOccurred())
m_Consumer->getTransaction()->setIssuedDiags(Transaction::kErrors);
if (CO.CodeCompletionOffset != -1) {
assert((int)SM.getFileOffset(PP.getCodeCompletionLoc())
== CO.CodeCompletionOffset
&& "Completion point wrongly set!");
assert(PP.isCodeCompletionReached()
&& "Code completion set but not reached!");
// Let's ignore this transaction:
m_Consumer->getTransaction()->setIssuedDiags(Transaction::kErrors);
return kSuccess;
}
LocalInstantiations.perform();
GlobalInstantiations.perform();
#ifdef _WIN32
// Microsoft-specific:
// Late parsed templates can leave unswallowed "macro"-like tokens.
// They will seriously confuse the Parser when entering the next
// source file. So lex until we are EOF.
Token Tok;
Tok.setKind(tok::eof);
do {
PP.Lex(Tok);
} while (Tok.isNot(tok::eof));
#endif
#ifndef NDEBUG
Token AssertTok;
PP.Lex(AssertTok);
assert(AssertTok.is(tok::eof) && "Lexer must be EOF when starting incremental parse!");
#endif
// Process any TopLevelDecls generated by #pragma weak.
for (llvm::SmallVector<Decl*,2>::iterator I = S.WeakTopLevelDecls().begin(),
E = S.WeakTopLevelDecls().end(); I != E; ++I) {
m_Consumer->HandleTopLevelDecl(DeclGroupRef(*I));
}
if (m_Consumer->getTransaction()->getIssuedDiags() == Transaction::kErrors)
return kFailed;
else if (Diags.getNumWarnings())
return kSuccessWithWarnings;
return kSuccess;
}
void IncrementalParser::printTransactionStructure() const {
for(size_t i = 0, e = m_Transactions.size(); i < e; ++i) {
m_Transactions[i]->printStructureBrief();
}
}
void IncrementalParser::SetTransformers(bool isChildInterpreter) {
// Add transformers to the IncrementalParser, which owns them
Sema* TheSema = &m_CI->getSema();
// if the interpreter compiles ptx code, some transformers should not be
// used
bool isCUDADevice = m_Interpreter->getOptions().CompilerOpts.CUDADevice;