| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "UnionToStruct.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Change a union declaration to a struct declaration. \ |
| 27 | For a union declaration which has only scalar fields, i.e., fields \ |
| 28 | with integer or pointer types, the transformation will set up an \ |
| 29 | appropriate initializer for a variable which is declared with a union \ |
| 30 | type under transformed: \n\ |
| 31 | * if originally a pointer field gets initialization value, then \ |
| 32 | other pointer fields with the same type will get the same initialization \ |
| 33 | value, and all other fields will be initialized to 0; \n\ |
| 34 | * if originally a integer field gets initialization value, then all \ |
| 35 | other integer fields will be initialized to the same value, while all \ |
| 36 | pointer fields will have 0. \n\ |
| 37 | \n\ |
| 38 | For other cases, all initializers will be dropped. (Note that this could \n\ |
| 39 | bring in uninitialized local variables.) \n"; |
| 40 | |
| 41 | static RegisterTransformation<UnionToStruct> |
| 42 | Trans("union-to-struct", DescriptionMsg); |
| 43 | |
| 44 | class UnionToStructCollectionVisitor : public |
| 45 | RecursiveASTVisitor<UnionToStructCollectionVisitor> { |
| 46 | |
| 47 | public: |
| 48 | explicit UnionToStructCollectionVisitor(UnionToStruct *Instance) |
| 49 | : ConsumerInstance(Instance) |
| 50 | { } |
| 51 | |
| 52 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 53 | |
| 54 | bool VisitVarDecl(VarDecl *VD); |
| 55 | |
| 56 | bool VisitFieldDecl(FieldDecl *FD); |
| 57 | |
| 58 | bool VisitRecordDecl(RecordDecl *RD); |
| 59 | |
| 60 | bool VisitDeclStmt(DeclStmt *DS); |
| 61 | |
| 62 | private: |
| 63 | |
| 64 | UnionToStruct *ConsumerInstance; |
| 65 | }; |
| 66 | |
| 67 | bool UnionToStructCollectionVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 68 | { |
| 69 | const Type *T = FD->getReturnType().getTypePtr(); |
| 70 | ConsumerInstance->addOneDeclarator(FD, T); |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | bool UnionToStructCollectionVisitor::VisitVarDecl(VarDecl *VD) |
| 75 | { |
| 76 | const Type *T = VD->getType().getTypePtr(); |
| 77 | ConsumerInstance->addOneDeclarator(VD, T); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool UnionToStructCollectionVisitor::VisitFieldDecl(FieldDecl *FD) |
| 82 | { |
| 83 | const Type *T = FD->getType().getTypePtr(); |
| 84 | ConsumerInstance->addOneDeclarator(FD, T); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool UnionToStructCollectionVisitor::VisitRecordDecl(RecordDecl *RD) |
| 89 | { |
| 90 | if (RD->isUnion() && !ConsumerInstance->isInIncludedFile(RD)) |
| 91 | ConsumerInstance->addOneRecord(RD); |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool UnionToStructCollectionVisitor::VisitDeclStmt(DeclStmt *DS) |
| 97 | { |
| 98 | for (DeclStmt::decl_iterator I = DS->decl_begin(), |
| 99 | E = DS->decl_end(); I != E; ++I) { |
| 100 | if (VarDecl *CurrVD = dyn_cast<VarDecl>(*I)) |
| 101 | ConsumerInstance->VarToDeclStmt[CurrVD] = DS; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | void UnionToStruct::Initialize(ASTContext &context) |
| 107 | { |
| 108 | Transformation::Initialize(context); |
| 109 | CollectionVisitor = new UnionToStructCollectionVisitor(this); |
| 110 | } |
| 111 | |
| 112 | bool UnionToStruct::HandleTopLevelDecl(DeclGroupRef D) |
| 113 | { |
| 114 | for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { |
| 115 | if (VarDecl *VD = dyn_cast<VarDecl>(*I)) |
| 116 | VarToDeclGroup[VD] = D; |
| 117 | |
| 118 | CollectionVisitor->TraverseDecl(*I); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | void UnionToStruct::HandleTranslationUnit(ASTContext &Ctx) |
| 124 | { |
| 125 | doAnalysis(); |
| 126 | if (QueryInstanceOnly) |
| 127 | return; |
| 128 | |
| 129 | if (TransformationCounter > ValidInstanceNum) { |
| 130 | TransError = TransMaxInstanceError; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | TransAssert(TheRecordDecl && "Cannot have NULL TheRecordDecl!"); |
| 135 | TransAssert(TheDeclaratorSet && "Cannot have NULL TheDeclaratorSet!"); |
| 136 | |
| 137 | rewriteRecordDecls(); |
| 138 | rewriteDeclarators(); |
| 139 | |
| 140 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 141 | |
| 142 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 143 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 144 | TransError = TransInternalError; |
| 145 | } |
| 146 | |
| 147 | bool UnionToStruct::isValidRecordDecl(const RecordDecl *RD) |
| 148 | { |
| 149 | if (!RD->isCompleteDefinition()) |
| 150 | return false; |
| 151 | |
| 152 | if (RD->isAnonymousStructOrUnion()) |
| 153 | return false; |
| 154 | |
| 155 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 156 | E = RD->field_end(); I != E; ++I) { |
| 157 | const FieldDecl *FD = (*I); |
| 158 | const Type *FDTy = FD->getType().getTypePtr(); |
| 159 | if (!FDTy->isScalarType()) |
| 160 | return false; |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | void UnionToStruct::doAnalysis(void) |
| 166 | { |
| 167 | for (RecordDeclToDeclaratorDeclMap::iterator I = RecordToDeclarator.begin(), |
| 168 | E = RecordToDeclarator.end(); I != E; ++I) { |
| 169 | ValidInstanceNum++; |
| 170 | if (ValidInstanceNum == TransformationCounter) { |
| 171 | // TheRecordDecl = ((*I).first)->getDefinition(); |
| 172 | TheRecordDecl = (*I).first; |
| 173 | TheDeclaratorSet = (*I).second; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void UnionToStruct::rewriteOneRecordDecl(const RecordDecl *RD) |
| 179 | { |
| 180 | TransAssert(RD && "NULL RecordDecl!"); |
| 181 | RewriteHelper->replaceUnionWithStruct(RD); |
| 182 | } |
| 183 | |
| 184 | void UnionToStruct::rewriteRecordDecls(void) |
| 185 | { |
| 186 | const RecordDecl *RD = |
| 187 | dyn_cast<RecordDecl>(TheRecordDecl->getCanonicalDecl()); |
| 188 | TransAssert(RD && "NULL RecordDecl!"); |
| 189 | for(RecordDecl::redecl_iterator I = RD->redecls_begin(), |
| 190 | E = RD->redecls_end(); I != E; ++I) { |
| 191 | rewriteOneRecordDecl(dyn_cast<RecordDecl>(*I)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void UnionToStruct::rewriteOneFieldDecl(const FieldDecl *FD) |
| 196 | { |
| 197 | const DeclContext *Ctx = TheRecordDecl->getLexicalParent(); |
| 198 | // Skip the case where we have: |
| 199 | // struct { |
| 200 | // union U { |
| 201 | // int f; |
| 202 | // } f1, f2; |
| 203 | // }; |
| 204 | // union U is handled by rewriteOneRecordDecl; |
| 205 | if (!dyn_cast<RecordDecl>(Ctx)) |
| 206 | RewriteHelper->replaceUnionWithStruct(FD); |
| 207 | } |
| 208 | |
| 209 | void UnionToStruct::getInitStrWithPointerType(const Expr *Exp, std::string &Str) |
| 210 | { |
| 211 | std::string ExprStr(""); |
| 212 | RewriteHelper->getExprString(Exp, ExprStr); |
| 213 | |
| 214 | RecordDecl::field_iterator I = TheRecordDecl->field_begin(); |
| 215 | RecordDecl::field_iterator E = TheRecordDecl->field_end(); |
| 216 | TransAssert((I != E) && "Empty RecordDecl!"); |
| 217 | Str = ExprStr; |
| 218 | ++I; |
| 219 | |
| 220 | if (I == E) |
| 221 | return; |
| 222 | |
| 223 | QualType ETy = Exp->getType().getCanonicalType(); |
| 224 | for (; I != E; ++I) { |
| 225 | Str += ","; |
| 226 | const FieldDecl *FD = (*I); |
| 227 | QualType FTy = FD->getType().getCanonicalType(); |
| 228 | if (ETy == FTy) |
| 229 | Str += ExprStr; |
| 230 | else |
| 231 | Str += "0"; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void UnionToStruct::getInitStrWithNonPointerType(const Expr *Exp, |
| 236 | std::string &Str) |
| 237 | { |
| 238 | std::string ExprStr(""); |
| 239 | RewriteHelper->getExprString(Exp, ExprStr); |
| 240 | |
| 241 | RecordDecl::field_iterator I = TheRecordDecl->field_begin(); |
| 242 | RecordDecl::field_iterator E = TheRecordDecl->field_end(); |
| 243 | TransAssert((I != E) && "Empty RecordDecl!"); |
| 244 | Str = ExprStr; |
| 245 | ++I; |
| 246 | |
| 247 | if (I == E) |
| 248 | return; |
| 249 | |
| 250 | for (; I != E; ++I) { |
| 251 | Str += ","; |
| 252 | const FieldDecl *FD = (*I); |
| 253 | const Type *FTy = FD->getType().getTypePtr(); |
| 254 | if (FTy->isPointerType()) |
| 255 | Str += "0"; |
| 256 | else |
| 257 | Str += ExprStr; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | bool UnionToStruct::isTheFirstDecl(const VarDecl *VD) |
| 262 | { |
| 263 | // always return false if VD is declared in a LinkageSpecDecl, |
| 264 | // because the first decl should be implicitly declared union record, |
| 265 | // which is handled by rewriteOneRecordDecl |
| 266 | const DeclContext *Ctx = VD->getDeclContext(); |
| 267 | if (dyn_cast<LinkageSpecDecl>(Ctx)) { |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | DeclGroupRef DGR; |
| 272 | if (const DeclStmt *DS = VarToDeclStmt[VD]) |
| 273 | DGR = DS->getDeclGroup(); |
| 274 | else |
| 275 | DGR = VarToDeclGroup[VD]; |
| 276 | |
| 277 | TransAssert(!DGR.isNull() && "Bad DeclRefGroup!"); |
| 278 | |
| 279 | if (DGR.isSingleDecl()) |
| 280 | return true; |
| 281 | |
| 282 | DeclGroupRef::const_iterator I = DGR.begin(); |
| 283 | const VarDecl *FirstVD = dyn_cast<VarDecl>(*I); |
| 284 | if (!FirstVD) |
| 285 | return false; |
| 286 | |
| 287 | return (VD == FirstVD); |
| 288 | } |
| 289 | |
| 290 | void UnionToStruct::rewriteOneVarDecl(const VarDecl *VD) |
| 291 | { |
| 292 | if (dyn_cast<ParmVarDecl>(VD)) { |
| 293 | RewriteHelper->replaceUnionWithStruct(VD); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | // If the first decl is RecordDecl, it will be handled |
| 298 | // by rewriteOneRecordDecl. |
| 299 | if (isTheFirstDecl(VD)) |
| 300 | RewriteHelper->replaceUnionWithStruct(VD); |
| 301 | |
| 302 | const Type *VDTy = VD->getType().getTypePtr(); |
| 303 | if (!VD->hasInit()) |
| 304 | return; |
| 305 | |
| 306 | if (const ArrayType *ArrayTy = dyn_cast<ArrayType>(VDTy)) { |
| 307 | VDTy = getArrayBaseElemType(ArrayTy); |
| 308 | // We remove the initializer for an array of unions |
| 309 | if (!VDTy->isUnionType()) { |
| 310 | return; |
| 311 | } |
| 312 | |
| 313 | const Expr *IE = VD->getInit(); |
| 314 | const InitListExpr *ILE = dyn_cast<InitListExpr>(IE); |
| 315 | if (!ILE) |
| 316 | return; |
| 317 | // handle a special case where we have code like this: |
| 318 | // union U a[][1] = {}; |
| 319 | // In this case, it's safe to keep the empty initializer |
| 320 | if (!ILE->getNumInits()) |
| 321 | return; |
| 322 | |
| 323 | RewriteHelper->removeVarInitExpr(VD); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | if (!VDTy->isUnionType()) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | const Expr *IE = VD->getInit(); |
| 332 | // Looks like we are safe to skip this |
| 333 | // e.g. |
| 334 | // union U { int x; }; |
| 335 | // void foo() { U y; } |
| 336 | if (dyn_cast<CXXConstructExpr>(IE)) |
| 337 | return; |
| 338 | if (!isValidRecordDecl(TheRecordDecl)) { |
| 339 | RewriteHelper->removeVarInitExpr(VD); |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(IE)) { |
| 344 | if (ILE->getNumInits() != 1) { |
| 345 | RewriteHelper->removeVarInitExpr(VD); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | const Expr *FirstE = ILE->getInit(0); |
| 350 | const Type *ExprTy = FirstE->getType().getTypePtr(); |
| 351 | std::string NewInitStr; |
| 352 | |
| 353 | if (ExprTy->isPointerType()) { |
| 354 | getInitStrWithPointerType(FirstE, NewInitStr); |
| 355 | } |
| 356 | else { |
| 357 | getInitStrWithNonPointerType(FirstE, NewInitStr); |
| 358 | } |
| 359 | |
| 360 | RewriteHelper->replaceExpr(FirstE, NewInitStr); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | void UnionToStruct::rewriteOneFunctionDecl(const FunctionDecl *FD) |
| 365 | { |
| 366 | RewriteHelper->replaceUnionWithStruct(FD); |
| 367 | } |
| 368 | |
| 369 | void UnionToStruct::rewriteDeclarators(void) |
| 370 | { |
| 371 | for (DeclaratorDeclSet::const_iterator I = TheDeclaratorSet->begin(), |
| 372 | E = TheDeclaratorSet->end(); I != E; ++I) { |
| 373 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(*I)) { |
| 374 | rewriteOneFieldDecl(FD); |
| 375 | continue; |
| 376 | } |
| 377 | |
| 378 | if (const FunctionDecl *FunD = dyn_cast<FunctionDecl>(*I)) { |
| 379 | rewriteOneFunctionDecl(FunD); |
| 380 | continue; |
| 381 | } |
| 382 | |
| 383 | const VarDecl *VD = dyn_cast<VarDecl>(*I); |
| 384 | TransAssert(VD && "Invalid Declarator kind!"); |
| 385 | rewriteOneVarDecl(VD); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void UnionToStruct::addOneDeclarator(const DeclaratorDecl *DD, const Type *T) |
| 390 | { |
| 391 | if (isInIncludedFile(DD)) |
| 392 | return; |
| 393 | |
| 394 | if (const ArrayType *ArrayTy = dyn_cast<ArrayType>(T)) |
| 395 | T = getArrayBaseElemType(ArrayTy); |
| 396 | |
| 397 | if (T->isPointerType()) |
| 398 | T = getBasePointerElemType(T); |
| 399 | |
| 400 | if (!T->isUnionType()) |
| 401 | return; |
| 402 | |
| 403 | if (T->getTypeClass() == Type::Typedef) |
| 404 | return; |
| 405 | |
| 406 | const RecordType *RDTy = T->getAsUnionType(); |
| 407 | TransAssert(RDTy && "Bad RecordType!"); |
| 408 | if (RDTy->isIncompleteType()) |
| 409 | return; |
| 410 | |
| 411 | const RecordDecl *RD = RDTy->getDecl(); |
| 412 | const RecordDecl *CanonicalRD = |
| 413 | dyn_cast<RecordDecl>(RD->getCanonicalDecl()); |
| 414 | TransAssert(CanonicalRD && "NULL CanonicalRD!"); |
| 415 | if (CanonicalRD->getNameAsString() == "") { |
| 416 | // this is a special case where we declare an unnamed union |
| 417 | // along with a function declaration. In this case, the DDSet |
| 418 | // for the RecordDecl hasn't been created, so we do it here. |
| 419 | // Also, because the routine for rewriting the RecordDecl will replace |
| 420 | // the unoin keywork, we don't need to keep DD in the DDSet. |
| 421 | addOneRecord(CanonicalRD); |
| 422 | return; |
| 423 | } |
| 424 | DeclaratorDeclSet *DDSet = RecordToDeclarator[CanonicalRD]; |
| 425 | // It's possible that we missed CanonicalRD in malformed code. |
| 426 | if (DDSet == nullptr) { |
| 427 | DDSet = addOneRecord(CanonicalRD); |
| 428 | } |
| 429 | DDSet->insert(DD); |
| 430 | } |
| 431 | |
| 432 | UnionToStruct::DeclaratorDeclSet *UnionToStruct::addOneRecord(const RecordDecl *RD) |
| 433 | { |
| 434 | const RecordDecl *CanonicalRD = |
| 435 | dyn_cast<RecordDecl>(RD->getCanonicalDecl()); |
| 436 | TransAssert(CanonicalRD && "NULL CanonicalRD!"); |
| 437 | DeclaratorDeclSet *DDSet = RecordToDeclarator[CanonicalRD]; |
| 438 | if (DDSet) |
| 439 | return DDSet; |
| 440 | |
| 441 | DDSet = new DeclaratorDeclSet(); |
| 442 | TransAssert(DDSet && "Member allocation failure!"); |
| 443 | RecordToDeclarator[CanonicalRD] = DDSet; |
| 444 | return DDSet; |
| 445 | } |
| 446 | |
| 447 | UnionToStruct::~UnionToStruct(void) |
| 448 | { |
| 449 | delete CollectionVisitor; |
| 450 | |
| 451 | for (RecordDeclToDeclaratorDeclMap::iterator I = RecordToDeclarator.begin(), |
| 452 | E = RecordToDeclarator.end(); I != E; ++I) { |
| 453 | delete (*I).second; |
| 454 | } |
| 455 | } |
| 456 |