update to removal of SourceManager::createFileID(FileEntry*)
[pet.git] / clang.cc
blob09514e05e6cd18d44f0ecdb524e0825b1eb22324
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.
60 * Look through any ElaboratedType sugar.
62 QualType pet_clang_base_or_typedef_type(QualType qt)
64 const Type *type = qt.getTypePtr();
66 if (isa<ElaboratedType>(type)) {
67 qt = cast<ElaboratedType>(type)->desugar();
68 return pet_clang_base_or_typedef_type(qt);
70 if (isa<TypedefType>(type))
71 return qt;
72 if (type->isPointerType())
73 return pet_clang_base_type(type->getPointeeType());
74 if (type->isArrayType()) {
75 const ArrayType *atype;
76 atype = cast<ArrayType>(type);
77 return pet_clang_base_type(atype->getElementType());
79 return qt;
82 /* Given a record type, return the corresponding RecordDecl.
84 RecordDecl *pet_clang_record_decl(QualType T)
86 const Type *type = T->getCanonicalTypeInternal().getTypePtr();
87 const RecordType *record;
88 record = cast<RecordType>(type);
89 return record->getDecl();
92 /* Strip off all outer casts from "expr" that are either implicit or a no-op.
94 Expr *pet_clang_strip_casts(Expr *expr)
96 while (isa<CastExpr>(expr)) {
97 CastExpr *ce = cast<CastExpr>(expr);
98 CastKind kind = ce->getCastKind();
99 if (!isa<ImplicitCastExpr>(expr) && kind != CK_NoOp)
100 break;
101 expr = ce->getSubExpr();
104 return expr;
107 /* Return the number of bits needed to represent the type "qt",
108 * if it is an integer type. Otherwise return 0.
109 * If qt is signed then return the opposite of the number of bits.
111 int pet_clang_get_type_size(QualType qt, ASTContext &ast_context)
113 int size;
115 if (!qt->isIntegerType())
116 return 0;
118 size = ast_context.getIntWidth(qt);
119 if (!qt->isUnsignedIntegerType())
120 size = -size;
122 return size;
125 /* Return the FunctionDecl that refers to the same function
126 * that "fd" refers to, but that has a body.
127 * Return NULL if no such FunctionDecl is available.
129 * It is not clear why hasBody takes a reference to a const FunctionDecl *.
130 * It seems that it is possible to directly use the iterators to obtain
131 * a non-const pointer.
132 * Since we are not going to use the pointer to modify anything anyway,
133 * it seems safe to drop the constness. The alternative would be to
134 * modify a lot of other functions to include const qualifiers.
136 FunctionDecl *pet_clang_find_function_decl_with_body(FunctionDecl *fd)
138 const FunctionDecl *def;
140 if (!fd->hasBody(def))
141 return NULL;
143 return const_cast<FunctionDecl *>(def);
146 /* Return the depth of an array of the given type.
148 int pet_clang_array_depth(QualType qt)
150 const Type *type = qt.getTypePtr();
152 if (type->isPointerType())
153 return 1 + pet_clang_array_depth(type->getPointeeType());
154 if (type->isArrayType()) {
155 const ArrayType *atype;
156 type = type->getCanonicalTypeInternal().getTypePtr();
157 atype = cast<ArrayType>(type);
158 return 1 + pet_clang_array_depth(atype->getElementType());
160 return 0;