README: update latest release of clang
[pet.git] / clang.cc
blob21edc316a88941fb8575bb042cf4d53fba6d5ed9
1 /*
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
7 * are met:
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
32 * Leiden University.
35 #include "clang.h"
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());
53 return qt;
56 /* Return the first typedef type that "qt" points to
57 * or the base type if there is no such typedef type.
58 * Do not call getCanonicalTypeInternal as in pet_clang_base_type
59 * because that throws away all internal typedef types.
61 QualType pet_clang_base_or_typedef_type(QualType qt)
63 const Type *type = qt.getTypePtr();
65 if (isa<TypedefType>(type))
66 return qt;
67 if (type->isPointerType())
68 return pet_clang_base_type(type->getPointeeType());
69 if (type->isArrayType()) {
70 const ArrayType *atype;
71 atype = cast<ArrayType>(type);
72 return pet_clang_base_type(atype->getElementType());
74 return qt;
77 /* Given a record type, return the corresponding RecordDecl.
79 RecordDecl *pet_clang_record_decl(QualType T)
81 const Type *type = T->getCanonicalTypeInternal().getTypePtr();
82 const RecordType *record;
83 record = cast<RecordType>(type);
84 return record->getDecl();
87 /* Strip off all outer casts from "expr" that are either implicit or a no-op.
89 Expr *pet_clang_strip_casts(Expr *expr)
91 while (isa<CastExpr>(expr)) {
92 CastExpr *ce = cast<CastExpr>(expr);
93 CastKind kind = ce->getCastKind();
94 if (!isa<ImplicitCastExpr>(expr) && kind != CK_NoOp)
95 break;
96 expr = ce->getSubExpr();
99 return expr;
102 /* Return the number of bits needed to represent the type "qt",
103 * if it is an integer type. Otherwise return 0.
104 * If qt is signed then return the opposite of the number of bits.
106 int pet_clang_get_type_size(QualType qt, ASTContext &ast_context)
108 int size;
110 if (!qt->isIntegerType())
111 return 0;
113 size = ast_context.getIntWidth(qt);
114 if (!qt->isUnsignedIntegerType())
115 size = -size;
117 return size;
120 /* Return the FunctionDecl that refers to the same function
121 * that "fd" refers to, but that has a body.
122 * Return NULL if no such FunctionDecl is available.
124 * It is not clear why hasBody takes a reference to a const FunctionDecl *.
125 * It seems that it is possible to directly use the iterators to obtain
126 * a non-const pointer.
127 * Since we are not going to use the pointer to modify anything anyway,
128 * it seems safe to drop the constness. The alternative would be to
129 * modify a lot of other functions to include const qualifiers.
131 FunctionDecl *pet_clang_find_function_decl_with_body(FunctionDecl *fd)
133 const FunctionDecl *def;
135 if (!fd->hasBody(def))
136 return NULL;
138 return const_cast<FunctionDecl *>(def);
141 /* Return the depth of an array of the given type.
143 int pet_clang_array_depth(QualType qt)
145 const Type *type = qt.getTypePtr();
147 if (type->isPointerType())
148 return 1 + pet_clang_array_depth(type->getPointeeType());
149 if (type->isArrayType()) {
150 const ArrayType *atype;
151 type = type->getCanonicalTypeInternal().getTypePtr();
152 atype = cast<ArrayType>(type);
153 return 1 + pet_clang_array_depth(atype->getElementType());
155 return 0;