-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclang-wrapper.cc
974 lines (872 loc) · 29.3 KB
/
clang-wrapper.cc
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
/****************************************************************************
* *
* GNATcoverage *
* *
* Copyright (C) 2021-2024, AdaCore *
* *
* GNATcoverage is free software; you can redistribute it and/or modify it *
* under terms of the GNU General Public License as published by the Free *
* Software Foundation; either version 3, or (at your option) any later *
* version. This software is distributed in the hope that it will be useful *
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public *
* License for more details. You should have received a copy of the GNU *
* General Public License distributed with this software; see file *
* COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy *
* of the license. *
* *
****************************************************************************/
/* Make sure we refer to the static version of symbols on Windows, not to DLL
importers. */
#define CINDEX_NO_EXPORTS
#include "libclang/CXCursor.h"
#include "libclang/CXSourceLocation.h"
#include "libclang/CXString.h"
#include "libclang/CXTranslationUnit.h"
#include "libclang/CursorVisitor.h"
#include "clang-c/Index.h"
#include "clang-c/Rewrite.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/ParentMapContext.h"
#include "clang/AST/StmtCXX.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/ASTUnit.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Token.h"
#include "clang/Rewrite/Core/Rewriter.h"
using namespace clang;
using namespace clang::cxcursor;
using namespace clang::cxloc;
using namespace clang::cxstring;
using namespace clang::cxtu;
/* Return the AST context corresponding to the given translation unit TU. */
static ASTContext &
getContext (CXTranslationUnit TU)
{
return getASTUnit (TU)->getASTContext ();
}
/* Likewise, but starting from a cursor. */
static ASTContext &
getContext (CXCursor C)
{
return getContext (getCursorTU (C));
}
/* Return the SourceManager corresponding to the given translation unit TU. */
static const SourceManager &
getSourceManager (CXTranslationUnit TU)
{
return getASTUnit (TU)->getSourceManager ();
}
/* Translate a source location to a cursor source location in the given
translation unit TU. */
static CXSourceLocation
translateSLoc (CXTranslationUnit TU, SourceLocation Loc)
{
return translateSourceLocation (getContext (TU), Loc);
}
/* Convert a clang Stmt type to a libclang CXCursor structure. The CXCursor C
is simply used to get a relevant declaration and translation unit to tie
the returned cursor to. */
CXCursor
MakeCXCursorWithNull (const Stmt *S, CXCursor C)
{
if (S)
return MakeCXCursor (S, getCursorDecl (C), getCursorTU (C));
else
return clang_getNullCursor ();
}
CXCursor
MakeCXCursorWithNull (const Decl *decl, CXCursor C)
{
if (decl)
return MakeCXCursor (decl, getCursorTU (C));
else
return clang_getNullCursor ();
}
/* Most of the functions are getters around existing clang functions of a
similar name, for the accepted type(s). For instance, clang_getCond will
simply call getCond on the given node, if this is a statement or an
expression that has this getter (an IfStmt, WhileStmt etc.). Some checks
are done, to make sure this is a supported type, before actually calling the
wrapped function on the casted node. */
extern "C" CXCursor
clang_getCond (CXCursor C)
{
if (clang_isStatement (C.kind) || clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::IfStmtClass:
return MakeCXCursorWithNull (cast<IfStmt> (S)->getCond (), C);
case Stmt::WhileStmtClass:
return MakeCXCursorWithNull (cast<WhileStmt> (S)->getCond (), C);
case Stmt::ForStmtClass:
return MakeCXCursorWithNull (cast<ForStmt> (S)->getCond (), C);
case Stmt::SwitchStmtClass:
return MakeCXCursorWithNull (cast<SwitchStmt> (S)->getCond (), C);
case Stmt::DoStmtClass:
return MakeCXCursorWithNull (cast<DoStmt> (S)->getCond (), C);
case Stmt::ConditionalOperatorClass:
return MakeCXCursorWithNull (
cast<ConditionalOperator> (S)->getCond (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getBody (CXCursor C)
{
if (clang_isDeclaration (C.kind))
{
if (const Decl *D = getCursorDecl (C))
switch (D->getKind ())
{
case Decl::FunctionTemplate:
return MakeCXCursorWithNull (
cast<FunctionTemplateDecl> (D)->getTemplatedDecl ()->getBody (),
C);
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
{
const FunctionDecl *FD = cast<FunctionDecl> (D);
if (FD->doesThisDeclarationHaveABody ())
return MakeCXCursorWithNull (FD->getBody (), C);
break;
}
default:
return MakeCXCursorWithNull (D->getBody (), C);
}
}
else if (clang_isStatement (C.kind))
{
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::WhileStmtClass:
return MakeCXCursorWithNull (cast<WhileStmt> (S)->getBody (), C);
case Stmt::ForStmtClass:
return MakeCXCursorWithNull (cast<ForStmt> (S)->getBody (), C);
case Stmt::CXXForRangeStmtClass:
return MakeCXCursorWithNull (cast<CXXForRangeStmt> (S)->getBody (),
C);
case Stmt::DoStmtClass:
return MakeCXCursorWithNull (cast<DoStmt> (S)->getBody (), C);
case Stmt::SwitchStmtClass:
return MakeCXCursorWithNull (cast<SwitchStmt> (S)->getBody (), C);
default:
return clang_getNullCursor ();
}
}
else if (clang_isExpression (C.kind))
if (const Expr *E = getCursorExpr (C))
switch (E->getStmtClass ())
{
case Expr::LambdaExprClass:
return MakeCXCursorWithNull (cast<LambdaExpr> (E)->getBody (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getForInit (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::ForStmtClass:
return MakeCXCursorWithNull (cast<ForStmt> (S)->getInit (), C);
case Stmt::CXXForRangeStmtClass:
return MakeCXCursorWithNull (cast<CXXForRangeStmt> (S)->getInit (),
C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getForRangeExpr (CXCursor C)
{
const Stmt *stmt;
const CXXForRangeStmt *for_stmt;
if (clang_isStatement (C.kind) && (stmt = cxcursor::getCursorStmt (C))
&& stmt->getStmtClass () == Stmt::CXXForRangeStmtClass)
{
for_stmt = cast<CXXForRangeStmt> (stmt);
return MakeCXCursorWithNull (for_stmt->getRangeInit (), C);
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getForInc (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::ForStmtClass:
return MakeCXCursorWithNull (cast<ForStmt> (S)->getInc (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getCondVar (CXCursor C)
{
const Stmt *stmt;
const WhileStmt *while_stmt;
const VarDecl *decl;
if (clang_isStatement (C.kind) && (stmt = getCursorStmt (C))
&& stmt->getStmtClass () == Stmt::WhileStmtClass)
{
while_stmt = cast<WhileStmt> (stmt);
if (decl = while_stmt->getConditionVariable ())
return MakeCXCursorWithNull (decl, C);
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getVarInitExpr (CXCursor C)
{
const Decl *decl;
const VarDecl *var_decl;
if (clang_isDeclaration (C.kind) && (decl = getCursorDecl (C))
&& decl->getKind () == Decl::Kind::Var)
{
var_decl = cast<VarDecl> (decl);
return MakeCXCursorWithNull (var_decl->getInit (), C);
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getThen (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::IfStmtClass:
return MakeCXCursorWithNull (cast<IfStmt> (S)->getThen (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getElse (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::IfStmtClass:
return MakeCXCursorWithNull (cast<IfStmt> (S)->getElse (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXSourceLocation
clang_getElseLoc (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::IfStmtClass:
return translateSLoc (getCursorTU (C),
cast<IfStmt> (S)->getElseLoc ());
default:
return clang_getNullLocation ();
}
return clang_getNullLocation ();
}
extern "C" CXSourceLocation
clang_getWhileLoc (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::DoStmtClass:
return translateSLoc (getCursorTU (C),
cast<DoStmt> (S)->getWhileLoc ());
default:
return clang_getNullLocation ();
}
return clang_getNullLocation ();
}
/* Given a clang::CompoundStmt, return the Location just after its left
bracket. Returns a null Location if the given argument is not as
expected. */
extern "C" CXSourceLocation
clang_getLBracLocPlusOne (CXCursor C)
{
const auto TU = getCursorTU (C);
const CompoundStmt *S
= dyn_cast_if_present<const CompoundStmt> (getCursorStmt (C));
if (!S)
return clang_getNullLocation ();
return translateSLoc (TU, S->getLBracLoc ().getLocWithOffset (1));
}
extern "C" bool
clang_isInstrumentableCallExpr (CXCursor C)
{
if (!clang_isExpression (C.kind))
return false;
// return C.kind == CXCursor_CallExpr;
const Expr *E = getCursorExpr (C);
switch (E->getStmtClass ())
{
case Stmt::CallExprClass: // Simple call expression
case Stmt::CXXOperatorCallExprClass: // C++ Overloaded operator call
case Stmt::CXXMemberCallExprClass: // C++ Method Call
// TODO??? All theses classes are regrouped under the CXCursor_Call_Expr
// kind. Decide what to do with them, so the call coverage
// instrumentation of Ctors and Dtors makes sense.
// case Stmt::CUDAKernelCallExprClass:
// case Stmt::CXXConstructExprClass:
// case Stmt::CXXInheritedCtorInitExprClass:
// case Stmt::CXXTemporaryObjectExprClass:
// case Stmt::CXXUnresolvedConstructExprClass:
// case Stmt::UserDefinedLiteralClass:
return true;
default:
return false;
}
}
extern "C" CXSourceRange
clang_getFunctionSignatureSloc (CXCursor C)
{
SourceLocation Begin (translateSourceLocation (clang_getNullLocation ()));
CompoundStmt *Func_Body = nullptr;
if (clang_isDeclaration (C.kind))
{
if (const Decl *D = getCursorDecl (C))
{
const FunctionDecl *FD = nullptr;
switch (D->getKind ())
{
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
FD = cast<FunctionDecl> (D);
break;
case Decl::FunctionTemplate:
FD = cast<FunctionTemplateDecl> (D)->getTemplatedDecl ();
break;
default:
// D is not a function declaration
break;
}
if (FD)
{
const SourceRange SR = FD->getSourceRange ();
Begin = SR.getBegin ();
Func_Body = cast<CompoundStmt> (FD->getBody ());
}
}
}
else if (clang_isExpression (C.kind))
{
if (const Expr *E = getCursorExpr (C))
{
switch (E->getStmtClass ())
{
case Expr::LambdaExprClass:
{
const LambdaExpr *LE = cast<LambdaExpr> (E);
const SourceRange SR = LE->getSourceRange ();
Begin = SR.getBegin ();
Func_Body = cast<CompoundStmt> (LE->getBody ());
}
break;
default:
// E is not a lambda expression
break;
}
}
}
// If Func_Body is still nullptr, the cursor is not a valid function
// declaration.
if (!Func_Body)
return clang_getNullRange ();
// ??? right now, `Func_Body->getBeginLoc` points on the opening bracket,
// which is not ideal. Ideally, we would cut the Source Range after the
// closing parenthesis (or after the `const`, or `throw` argument).
return translateSourceRange (getContext (C),
SourceRange (Begin, Func_Body->getBeginLoc ()));
}
extern "C" CXCursor
clang_getSubExpr (CXCursor C)
{
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::UnaryOperatorClass:
return MakeCXCursorWithNull (cast<UnaryOperator> (S)->getSubExpr (),
C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getSubStmt (CXCursor C)
{
if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::CaseStmtClass:
return MakeCXCursorWithNull (cast<CaseStmt> (S)->getSubStmt (), C);
case Stmt::DefaultStmtClass:
return MakeCXCursorWithNull (cast<DefaultStmt> (S)->getSubStmt (),
C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getRHS (CXCursor C)
{
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::BinaryOperatorClass:
return MakeCXCursorWithNull (cast<BinaryOperator> (S)->getRHS (), C);
case Stmt::ConditionalOperatorClass:
return MakeCXCursorWithNull (
cast<ConditionalOperator> (S)->getRHS (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
extern "C" CXCursor
clang_getLHS (CXCursor C)
{
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::BinaryOperatorClass:
return MakeCXCursorWithNull (cast<BinaryOperator> (S)->getLHS (), C);
case Stmt::ConditionalOperatorClass:
return MakeCXCursorWithNull (
cast<ConditionalOperator> (S)->getLHS (), C);
default:
return clang_getNullCursor ();
}
return clang_getNullCursor ();
}
/* Return the name of the declared object. */
extern "C" const CXString
clang_getDeclName (CXCursor C)
{
if (clang_isDeclaration (C.kind))
{
auto getFunctionDeclName = [] (const FunctionDecl *FD) {
const DeclarationName FunctionName = FD->getNameInfo ().getName ();
return createDup (FunctionName.getAsString ().c_str ());
};
const Decl *D = getCursorDecl (C);
switch (D->getKind ())
{
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
{
const clang::FunctionDecl *FD = cast<clang::FunctionDecl> (D);
return getFunctionDeclName (FD);
}
case Decl::FunctionTemplate:
{
const clang::FunctionDecl *FD
= (cast<clang::FunctionTemplateDecl> (D))->getTemplatedDecl ();
return getFunctionDeclName (FD);
}
case Decl::Namespace:
{
const NamespaceDecl *ND
= (cast<clang::NamespaceDecl> (D))->getCanonicalDecl ();
if (ND->isAnonymousNamespace ())
return createDup ("Anonymous namespace");
return createDup (ND->getName ());
}
case Decl::ClassTemplate:
{
const clang::ClassTemplateDecl *CD
= (cast<clang::ClassTemplateDecl> (D))->getCanonicalDecl ();
return createDup (CD->getName ());
}
case Decl::CXXRecord:
{
const clang::CXXRecordDecl *RD
= (cast<clang::CXXRecordDecl> (D))->getCanonicalDecl ();
return createDup (RD->getName ());
}
default:
return createEmpty ();
}
}
return createEmpty ();
}
/* Return the name of the callee. */
extern "C" const CXString
clang_getCalleeName (CXCursor C)
{
if (clang_isExpression (C.kind))
{
const Expr *E = getCursorExpr (C);
if (E->getStmtClass () == Stmt::CallExprClass)
{
const clang::CallExpr *CE = cast<clang::CallExpr> (E);
const Decl *D = CE->getCalleeDecl ();
if (!D)
return createEmpty ();
auto getFunctionDeclName = [] (const FunctionDecl *FD) {
const DeclarationName FunctionName = FD->getNameInfo ().getName ();
return createDup (FunctionName.getAsString ().c_str ());
};
switch (D->getKind ())
{
case Decl::FunctionTemplate:
return getFunctionDeclName (
(cast<clang::FunctionTemplateDecl> (D))->getTemplatedDecl ());
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
return getFunctionDeclName (cast<clang::FunctionDecl> (D));
default:
return createEmpty ();
}
}
}
return createEmpty ();
}
extern "C" bool
clang_isThisDeclarationADefinition (CXCursor C)
{
if (!clang_isDeclaration (C.kind))
{
return false;
}
const Decl *D = getCursorDecl (C);
switch (D->getKind ())
{
case Decl::FunctionTemplate:
return cast<FunctionTemplateDecl> (D)
->getTemplatedDecl ()
->isThisDeclarationADefinition ();
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
case Decl::CXXConversion:
return cast<FunctionDecl> (D)->isThisDeclarationADefinition ();
default:
return false;
}
}
/* Given a Decl_Stmt, return the only declaration if it is a single decl,
and the first of the declaration list otherwise. */
extern "C" CXCursor
clang_getFirstDecl (CXCursor C)
{
if (clang_isStatement (C.kind))
{
if (const Stmt *S = getCursorStmt (C))
{
if (S->getStmtClass () != Stmt::DeclStmtClass)
return clang_getNullCursor ();
auto DGR = llvm::cast<DeclStmt> (S)->getDeclGroup ();
if (DGR.isSingleDecl ())
return MakeCXCursorWithNull (DGR.getSingleDecl (), C);
auto DI = DGR.begin ();
assert (DI != DGR.end () && "Unexpected empty DeclGroup");
return MakeCXCursorWithNull (*DI, C);
}
}
return clang_getNullCursor ();
}
extern "C" unsigned
clang_isConstexpr (CXCursor C)
{
if (clang_isDeclaration (C.kind))
{
if (const Decl *D = getCursorDecl (C))
{
switch (D->getKind ())
{
case Decl::FunctionTemplate:
{
const clang::FunctionDecl *FD
= (cast<clang::FunctionTemplateDecl> (D))
->getTemplatedDecl ();
return FD->isConstexpr () ? 1 : 0;
}
case Decl::Function:
case Decl::CXXMethod:
case Decl::CXXConstructor:
case Decl::CXXDestructor:
{
const FunctionDecl *FD = cast<FunctionDecl> (D);
return FD->isConstexpr () ? 1 : 0;
}
case Decl::Kind::Var:
const VarDecl *VD = cast<VarDecl> (D);
return VD->isConstexpr () ? 1 : 0;
}
}
}
else if (clang_isStatement (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::IfStmtClass:
{
const IfStmt *ifStmt = cast<IfStmt> (S);
return ifStmt->isConstexpr () ? 1 : 0;
}
case Stmt::DeclStmtClass:
{
// For DeclStmt, consider it a constexpr if any of the its
// variable declaration is. This avoids spurious checking on the
// C instrumentation side.
const DeclStmt *declStmt = cast<DeclStmt> (S);
for (const auto *innerDecl : declStmt->decls ())
if (const auto *varDecl = dyn_cast<VarDecl> (innerDecl))
return varDecl->isConstexpr () ? 1 : 0;
}
}
return 0;
}
/* Return the string representative of the operator for a binary
or unary operator node. */
extern "C" CXString
clang_getOpcodeStr (CXCursor C)
{
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::BinaryOperatorClass:
return createRef (BinaryOperator::getOpcodeStr (
cast<BinaryOperator> (S)->getOpcode ()));
case Stmt::UnaryOperatorClass:
return createRef (UnaryOperator::getOpcodeStr (
cast<UnaryOperator> (S)->getOpcode ()));
default:
return createEmpty ();
}
return createEmpty ();
}
/* Return the location of the operator for a binary / unary
* operator node. */
extern "C" CXSourceLocation
clang_getOperatorLoc (CXCursor C)
{
CXTranslationUnit TU = getCursorTU (C);
SourceLocation sloc;
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::BinaryOperatorClass:
sloc = cast<BinaryOperator> (S)->getOperatorLoc ();
break;
case Stmt::UnaryOperatorClass:
sloc = cast<UnaryOperator> (S)->getOperatorLoc ();
break;
default:
return clang_getNullLocation ();
}
return translateSLoc (TU, sloc);
}
/* If the given expression is a wrapping expression (i.e. a
parenthesized expression, a cast expression etc.), return the
outermost expression inside that is not a wrapping expression.
*/
extern "C" CXCursor
clang_unwrap (CXCursor C)
{
if (clang_isExpression (C.kind))
if (const Stmt *S = getCursorStmt (C))
switch (S->getStmtClass ())
{
case Stmt::ParenExprClass:
return clang_unwrap (
MakeCXCursorWithNull (cast<ParenExpr> (S)->getSubExpr (), C));
case Expr::ConstantExprClass:
case Expr::ExprWithCleanupsClass:
return clang_unwrap (
MakeCXCursorWithNull (cast<FullExpr> (S)->getSubExpr (), C));
case Expr::ImplicitCastExprClass:
case Expr::CStyleCastExprClass:
case Expr::CXXFunctionalCastExprClass:
case Expr::CXXStaticCastExprClass:
case Expr::CXXDynamicCastExprClass:
case Expr::CXXReinterpretCastExprClass:
case Expr::CXXConstCastExprClass:
case Expr::CXXAddrspaceCastExprClass:
return clang_unwrap (
MakeCXCursorWithNull (cast<CastExpr> (S)->getSubExpr (), C));
}
return C;
}
/* Visit a node, starting by itself (in contrary to
clang_visitChildren that immediately starts with the children
of the node). */
extern "C" unsigned
clang_visit (CXCursor parent, CXCursorVisitor visitor,
CXClientData client_data)
{
CursorVisitor CursorVis (getCursorTU (parent), visitor, client_data,
/*VisitPreprocessorLast=*/false);
return CursorVis.Visit (parent);
}
/* Returns the parent node of the given node. This accepts
statements and expressions, unlike clang_getLexicalParent and
clang_getCursorParent. */
extern "C" CXCursor
clang_getParent (CXCursor C)
{
assert (clang_isStatement (C.kind) || clang_isExpression (C.kind));
if (const Stmt *S = getCursorStmt (C))
{
const auto Parents = getContext (C).getParents (*S);
if (Parents.empty ())
return clang_getNullCursor ();
const auto &SParent = Parents[0];
if (const auto *Res = SParent.get<Stmt> ())
return MakeCXCursorWithNull (Res, C);
}
return clang_getNullCursor ();
}
/* Wrappers around rewriting functions. */
extern "C" void
clang_CXRewriter_insertTextAfter (CXRewriter Rew, CXSourceLocation Loc,
const char *Insert)
{
assert (Rew);
Rewriter &R = *reinterpret_cast<Rewriter *> (Rew);
R.InsertTextAfter (translateSourceLocation (Loc), Insert);
}
extern "C" void
clang_CXRewriter_insertTextAfterToken (CXRewriter Rew, CXSourceLocation Loc,
const char *Insert)
{
assert (Rew);
Rewriter &R = *reinterpret_cast<Rewriter *> (Rew);
R.InsertTextAfterToken (translateSourceLocation (Loc), Insert);
}
extern "C" void
clang_CXRewriter_insertTextBeforeToken (CXRewriter Rew, CXSourceLocation Loc,
const char *Insert)
{
assert (Rew);
Rewriter &R = *reinterpret_cast<Rewriter *> (Rew);
// Get the previous location, and insert after it
SourceLocation SLoc = translateSourceLocation (Loc);
SourceLocation Prv = SLoc.getLocWithOffset (-1);
R.InsertTextAfter (Prv, Insert);
}
extern "C" CXString
clang_CXRewriter_getRewrittenText (CXRewriter Rew, CXSourceRange Rng)
{
assert (Rew);
Rewriter &R = *reinterpret_cast<Rewriter *> (Rew);
SourceRange SR = translateCXSourceRange (Rng);
return createDup (R.getRewrittenText (SR).c_str ());
}
/* Wrappers around source location analysis functions. */
extern "C" unsigned
clang_isMacroLocation (CXSourceLocation Loc)
{
const SourceLocation SLoc = translateSourceLocation (Loc);
return SLoc.isMacroID () ? 1 : 0;
}
extern "C" unsigned
clang_isMacroArgExpansion (CXSourceLocation Loc, CXSourceLocation *StartLoc,
CXTranslationUnit TU)
{
const SourceManager &SM = getSourceManager (TU);
const SourceLocation SLoc = translateSourceLocation (Loc);
SourceLocation Result;
if (SM.isMacroArgExpansion (SLoc, &Result))
{
*StartLoc = translateSLoc (TU, Result);
return 1;
}
return 0;
}
extern "C" CXSourceLocation
clang_getImmediateMacroCallerLoc (CXSourceLocation Loc, CXTranslationUnit TU)
{
const SourceManager &SM = getSourceManager (TU);
SourceLocation SLoc = translateSourceLocation (Loc);
if (SLoc.isMacroID ())
return translateSLoc (TU, SM.getImmediateMacroCallerLoc (SLoc));
return Loc;
}
extern "C" CXSourceLocation
clang_getImmediateExpansionLoc (CXSourceLocation Loc, CXTranslationUnit TU)
{
const SourceManager &SM = getSourceManager (TU);
SourceLocation SLoc = translateSourceLocation (Loc);
return translateSLoc (TU, SM.getImmediateExpansionRange (SLoc).getBegin ());
}
extern "C" CXString
clang_getImmediateMacroNameForDiagnostics (CXSourceLocation Loc,
CXTranslationUnit TU)
{
SourceLocation SLoc = translateSourceLocation (Loc);
const SourceManager &SM = getSourceManager (TU);
return createDup (Lexer::getImmediateMacroNameForDiagnostics (
SLoc, SM, getContext (TU).getLangOpts ()));
}
extern "C" CXSourceLocation
clang_getExpansionEnd (CXTranslationUnit TU, CXSourceLocation Loc)
{
SourceLocation SLoc = translateSourceLocation (Loc);
const SourceManager &SM = getSourceManager (TU);
return translateSLoc (TU, SM.getExpansionRange (SLoc).getEnd ());
}
extern "C" CXTranslationUnit
clang_getCursorTU (CXCursor C)
{
return getCursorTU (C);
}
/* Debug helpers. */
extern "C" void
clang_printLocation (CXTranslationUnit TU, CXSourceLocation Loc)
{
const SourceManager &SM = getSourceManager (TU);
translateSourceLocation (Loc).dump (SM);
}
extern "C" CXSourceLocation
clang_getSpellingLoc (CXTranslationUnit TU, CXSourceLocation location)
{
SourceLocation Loc = SourceLocation::getFromRawEncoding (location.int_data);
if (!location.ptr_data[0] || Loc.isInvalid ())
return clang_getNullLocation ();
const SourceManager &SM = getSourceManager (TU);
return translateSLoc (TU, SM.getSpellingLoc (Loc));
}