2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
37 using namespace clang
;
39 /* Return the element type of the given array type.
41 QualType
pet_clang_base_type(QualType qt
)
43 const Type
*type
= qt
.getTypePtr();
45 if (type
->isPointerType())
46 return pet_clang_base_type(type
->getPointeeType());
47 if (type
->isArrayType()) {
48 const ArrayType
*atype
;
49 type
= type
->getCanonicalTypeInternal().getTypePtr();
50 atype
= cast
<ArrayType
>(type
);
51 return pet_clang_base_type(atype
->getElementType());
56 /* Given a record type, return the corresponding RecordDecl.
58 RecordDecl
*pet_clang_record_decl(QualType T
)
60 const Type
*type
= T
->getCanonicalTypeInternal().getTypePtr();
61 const RecordType
*record
;
62 record
= cast
<RecordType
>(type
);
63 return record
->getDecl();
66 /* Strip off all outer casts from "expr" that are either implicit or a no-op.
68 Expr
*pet_clang_strip_casts(Expr
*expr
)
70 while (isa
<CastExpr
>(expr
)) {
71 CastExpr
*ce
= cast
<CastExpr
>(expr
);
72 CastKind kind
= ce
->getCastKind();
73 if (!isa
<ImplicitCastExpr
>(expr
) && kind
!= CK_NoOp
)
75 expr
= ce
->getSubExpr();
81 /* Return the number of bits needed to represent the type "qt",
82 * if it is an integer type. Otherwise return 0.
83 * If qt is signed then return the opposite of the number of bits.
85 int pet_clang_get_type_size(QualType qt
, ASTContext
&ast_context
)
89 if (!qt
->isIntegerType())
92 size
= ast_context
.getIntWidth(qt
);
93 if (!qt
->isUnsignedIntegerType())
99 /* Return the FunctionDecl that refers to the same function
100 * that "fd" refers to, but that has a body.
101 * Return NULL if no such FunctionDecl is available.
103 * It is not clear why hasBody takes a reference to a const FunctionDecl *.
104 * It seems that it is possible to directly use the iterators to obtain
105 * a non-const pointer.
106 * Since we are not going to use the pointer to modify anything anyway,
107 * it seems safe to drop the constness. The alternative would be to
108 * modify a lot of other functions to include const qualifiers.
110 FunctionDecl
*pet_clang_find_function_decl_with_body(FunctionDecl
*fd
)
112 const FunctionDecl
*def
;
114 if (!fd
->hasBody(def
))
117 return const_cast<FunctionDecl
*>(def
);
120 /* Return the depth of an array of the given type.
122 int pet_clang_array_depth(QualType qt
)
124 const Type
*type
= qt
.getTypePtr();
126 if (type
->isPointerType())
127 return 1 + pet_clang_array_depth(type
->getPointeeType());
128 if (type
->isArrayType()) {
129 const ArrayType
*atype
;
130 type
= type
->getCanonicalTypeInternal().getTypePtr();
131 atype
= cast
<ArrayType
>(type
);
132 return 1 + pet_clang_array_depth(atype
->getElementType());