1 //===-- DataflowEnvironment.cpp ---------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines an Environment class that is used by dataflow analyses 10 // that run over Control-Flow Graphs (CFGs) to keep track of the state of the 11 // program at given program points. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Analysis/FlowSensitive/DataflowEnvironment.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/Type.h" 19 #include "clang/Analysis/FlowSensitive/DataflowLattice.h" 20 #include "clang/Analysis/FlowSensitive/Value.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/DenseSet.h" 23 #include "llvm/ADT/MapVector.h" 24 #include "llvm/ADT/STLExtras.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include <cassert> 27 #include <utility> 28 29 namespace clang { 30 namespace dataflow { 31 32 // FIXME: convert these to parameters of the analysis or environment. Current 33 // settings have been experimentaly validated, but only for a particular 34 // analysis. 35 static constexpr int MaxCompositeValueDepth = 3; 36 static constexpr int MaxCompositeValueSize = 1000; 37 38 /// Returns a map consisting of key-value entries that are present in both maps. 39 static llvm::DenseMap<const ValueDecl *, StorageLocation *> intersectDeclToLoc( 40 const llvm::DenseMap<const ValueDecl *, StorageLocation *> &DeclToLoc1, 41 const llvm::DenseMap<const ValueDecl *, StorageLocation *> &DeclToLoc2) { 42 llvm::DenseMap<const ValueDecl *, StorageLocation *> Result; 43 for (auto &Entry : DeclToLoc1) { 44 auto It = DeclToLoc2.find(Entry.first); 45 if (It != DeclToLoc2.end() && Entry.second == It->second) 46 Result.insert({Entry.first, Entry.second}); 47 } 48 return Result; 49 } 50 51 // Performs a join on either `ExprToLoc` or `ExprToVal`. 52 // The maps must be consistent in the sense that any entries for the same 53 // expression must map to the same location / value. This is the case if we are 54 // performing a join for control flow within a full-expression (which is the 55 // only case when this function should be used). 56 template <typename MapT> MapT joinExprMaps(const MapT &Map1, const MapT &Map2) { 57 MapT Result = Map1; 58 59 for (const auto &Entry : Map2) { 60 [[maybe_unused]] auto [It, Inserted] = Result.insert(Entry); 61 // If there was an existing entry, its value should be the same as for the 62 // entry we were trying to insert. 63 assert(It->second == Entry.second); 64 } 65 66 return Result; 67 } 68 69 // Whether to consider equivalent two values with an unknown relation. 70 // 71 // FIXME: this function is a hack enabling unsoundness to support 72 // convergence. Once we have widening support for the reference/pointer and 73 // struct built-in models, this should be unconditionally `false` (and inlined 74 // as such at its call sites). 75 static bool equateUnknownValues(Value::Kind K) { 76 switch (K) { 77 case Value::Kind::Integer: 78 case Value::Kind::Pointer: 79 case Value::Kind::Record: 80 return true; 81 default: 82 return false; 83 } 84 } 85 86 static bool compareDistinctValues(QualType Type, Value &Val1, 87 const Environment &Env1, Value &Val2, 88 const Environment &Env2, 89 Environment::ValueModel &Model) { 90 // Note: Potentially costly, but, for booleans, we could check whether both 91 // can be proven equivalent in their respective environments. 92 93 // FIXME: move the reference/pointers logic from `areEquivalentValues` to here 94 // and implement separate, join/widen specific handling for 95 // reference/pointers. 96 switch (Model.compare(Type, Val1, Env1, Val2, Env2)) { 97 case ComparisonResult::Same: 98 return true; 99 case ComparisonResult::Different: 100 return false; 101 case ComparisonResult::Unknown: 102 return equateUnknownValues(Val1.getKind()); 103 } 104 llvm_unreachable("All cases covered in switch"); 105 } 106 107 /// Attempts to join distinct values `Val1` and `Val2` in `Env1` and `Env2`, 108 /// respectively, of the same type `Type`. Joining generally produces a single 109 /// value that (soundly) approximates the two inputs, although the actual 110 /// meaning depends on `Model`. 111 static Value *joinDistinctValues(QualType Type, Value &Val1, 112 const Environment &Env1, Value &Val2, 113 const Environment &Env2, 114 Environment &JoinedEnv, 115 Environment::ValueModel &Model) { 116 // Join distinct boolean values preserving information about the constraints 117 // in the respective path conditions. 118 if (isa<BoolValue>(&Val1) && isa<BoolValue>(&Val2)) { 119 // FIXME: Checking both values should be unnecessary, since they should have 120 // a consistent shape. However, right now we can end up with BoolValue's in 121 // integer-typed variables due to our incorrect handling of 122 // boolean-to-integer casts (we just propagate the BoolValue to the result 123 // of the cast). So, a join can encounter an integer in one branch but a 124 // bool in the other. 125 // For example: 126 // ``` 127 // std::optional<bool> o; 128 // int x; 129 // if (o.has_value()) 130 // x = o.value(); 131 // ``` 132 auto &Expr1 = cast<BoolValue>(Val1).formula(); 133 auto &Expr2 = cast<BoolValue>(Val2).formula(); 134 auto &A = JoinedEnv.arena(); 135 auto &JoinedVal = A.makeAtomRef(A.makeAtom()); 136 JoinedEnv.assume( 137 A.makeOr(A.makeAnd(A.makeAtomRef(Env1.getFlowConditionToken()), 138 A.makeEquals(JoinedVal, Expr1)), 139 A.makeAnd(A.makeAtomRef(Env2.getFlowConditionToken()), 140 A.makeEquals(JoinedVal, Expr2)))); 141 return &A.makeBoolValue(JoinedVal); 142 } 143 144 Value *JoinedVal = nullptr; 145 if (auto *RecordVal1 = dyn_cast<RecordValue>(&Val1)) { 146 auto *RecordVal2 = cast<RecordValue>(&Val2); 147 148 if (&RecordVal1->getLoc() == &RecordVal2->getLoc()) 149 // `RecordVal1` and `RecordVal2` may have different properties associated 150 // with them. Create a new `RecordValue` with the same location but 151 // without any properties so that we soundly approximate both values. If a 152 // particular analysis needs to join properties, it should do so in 153 // `DataflowAnalysis::join()`. 154 JoinedVal = &JoinedEnv.create<RecordValue>(RecordVal1->getLoc()); 155 else 156 // If the locations for the two records are different, need to create a 157 // completely new value. 158 JoinedVal = JoinedEnv.createValue(Type); 159 } else { 160 JoinedVal = JoinedEnv.createValue(Type); 161 } 162 163 if (JoinedVal) 164 Model.join(Type, Val1, Env1, Val2, Env2, *JoinedVal, JoinedEnv); 165 166 return JoinedVal; 167 } 168 169 // When widening does not change `Current`, return value will equal `&Prev`. 170 static Value &widenDistinctValues(QualType Type, Value &Prev, 171 const Environment &PrevEnv, Value &Current, 172 Environment &CurrentEnv, 173 Environment::ValueModel &Model) { 174 // Boolean-model widening. 175 if (auto *PrevBool = dyn_cast<BoolValue>(&Prev)) { 176 // If previous value was already Top, re-use that to (implicitly) indicate 177 // that no change occurred. 178 if (isa<TopBoolValue>(Prev)) 179 return Prev; 180 181 // We may need to widen to Top, but before we do so, check whether both 182 // values are implied to be either true or false in the current environment. 183 // In that case, we can simply return a literal instead. 184 auto &CurBool = cast<BoolValue>(Current); 185 bool TruePrev = PrevEnv.proves(PrevBool->formula()); 186 bool TrueCur = CurrentEnv.proves(CurBool.formula()); 187 if (TruePrev && TrueCur) 188 return CurrentEnv.getBoolLiteralValue(true); 189 if (!TruePrev && !TrueCur && 190 PrevEnv.proves(PrevEnv.arena().makeNot(PrevBool->formula())) && 191 CurrentEnv.proves(CurrentEnv.arena().makeNot(CurBool.formula()))) 192 return CurrentEnv.getBoolLiteralValue(false); 193 194 return CurrentEnv.makeTopBoolValue(); 195 } 196 197 // FIXME: Add other built-in model widening. 198 199 // Custom-model widening. 200 if (auto *W = Model.widen(Type, Prev, PrevEnv, Current, CurrentEnv)) 201 return *W; 202 203 return equateUnknownValues(Prev.getKind()) ? Prev : Current; 204 } 205 206 // Returns whether the values in `Map1` and `Map2` compare equal for those 207 // keys that `Map1` and `Map2` have in common. 208 template <typename Key> 209 bool compareKeyToValueMaps(const llvm::MapVector<Key, Value *> &Map1, 210 const llvm::MapVector<Key, Value *> &Map2, 211 const Environment &Env1, const Environment &Env2, 212 Environment::ValueModel &Model) { 213 for (auto &Entry : Map1) { 214 Key K = Entry.first; 215 assert(K != nullptr); 216 217 Value *Val = Entry.second; 218 assert(Val != nullptr); 219 220 auto It = Map2.find(K); 221 if (It == Map2.end()) 222 continue; 223 assert(It->second != nullptr); 224 225 if (!areEquivalentValues(*Val, *It->second) && 226 !compareDistinctValues(K->getType(), *Val, Env1, *It->second, Env2, 227 Model)) 228 return false; 229 } 230 231 return true; 232 } 233 234 // Perform a join on two `LocToVal` maps. 235 static llvm::MapVector<const StorageLocation *, Value *> 236 joinLocToVal(const llvm::MapVector<const StorageLocation *, Value *> &LocToVal, 237 const llvm::MapVector<const StorageLocation *, Value *> &LocToVal2, 238 const Environment &Env1, const Environment &Env2, 239 Environment &JoinedEnv, Environment::ValueModel &Model) { 240 llvm::MapVector<const StorageLocation *, Value *> Result; 241 for (auto &Entry : LocToVal) { 242 const StorageLocation *Loc = Entry.first; 243 assert(Loc != nullptr); 244 245 Value *Val = Entry.second; 246 assert(Val != nullptr); 247 248 auto It = LocToVal2.find(Loc); 249 if (It == LocToVal2.end()) 250 continue; 251 assert(It->second != nullptr); 252 253 if (areEquivalentValues(*Val, *It->second)) { 254 Result.insert({Loc, Val}); 255 continue; 256 } 257 258 if (Value *JoinedVal = joinDistinctValues( 259 Loc->getType(), *Val, Env1, *It->second, Env2, JoinedEnv, Model)) { 260 Result.insert({Loc, JoinedVal}); 261 } 262 } 263 264 return Result; 265 } 266 267 // Perform widening on either `LocToVal` or `ExprToVal`. `Key` must be either 268 // `const StorageLocation *` or `const Expr *`. 269 template <typename Key> 270 llvm::MapVector<Key, Value *> 271 widenKeyToValueMap(const llvm::MapVector<Key, Value *> &CurMap, 272 const llvm::MapVector<Key, Value *> &PrevMap, 273 Environment &CurEnv, const Environment &PrevEnv, 274 Environment::ValueModel &Model, LatticeJoinEffect &Effect) { 275 llvm::MapVector<Key, Value *> WidenedMap; 276 for (auto &Entry : CurMap) { 277 Key K = Entry.first; 278 assert(K != nullptr); 279 280 Value *Val = Entry.second; 281 assert(Val != nullptr); 282 283 auto PrevIt = PrevMap.find(K); 284 if (PrevIt == PrevMap.end()) 285 continue; 286 assert(PrevIt->second != nullptr); 287 288 if (areEquivalentValues(*Val, *PrevIt->second)) { 289 WidenedMap.insert({K, Val}); 290 continue; 291 } 292 293 Value &WidenedVal = widenDistinctValues(K->getType(), *PrevIt->second, 294 PrevEnv, *Val, CurEnv, Model); 295 WidenedMap.insert({K, &WidenedVal}); 296 if (&WidenedVal != PrevIt->second) 297 Effect = LatticeJoinEffect::Changed; 298 } 299 300 return WidenedMap; 301 } 302 303 /// Initializes a global storage value. 304 static void insertIfGlobal(const Decl &D, 305 llvm::DenseSet<const VarDecl *> &Vars) { 306 if (auto *V = dyn_cast<VarDecl>(&D)) 307 if (V->hasGlobalStorage()) 308 Vars.insert(V); 309 } 310 311 static void insertIfFunction(const Decl &D, 312 llvm::DenseSet<const FunctionDecl *> &Funcs) { 313 if (auto *FD = dyn_cast<FunctionDecl>(&D)) 314 Funcs.insert(FD); 315 } 316 317 static MemberExpr *getMemberForAccessor(const CXXMemberCallExpr &C) { 318 // Use getCalleeDecl instead of getMethodDecl in order to handle 319 // pointer-to-member calls. 320 const auto *MethodDecl = dyn_cast_or_null<CXXMethodDecl>(C.getCalleeDecl()); 321 if (!MethodDecl) 322 return nullptr; 323 auto *Body = dyn_cast_or_null<CompoundStmt>(MethodDecl->getBody()); 324 if (!Body || Body->size() != 1) 325 return nullptr; 326 if (auto *RS = dyn_cast<ReturnStmt>(*Body->body_begin())) 327 if (auto *Return = RS->getRetValue()) 328 return dyn_cast<MemberExpr>(Return->IgnoreParenImpCasts()); 329 return nullptr; 330 } 331 332 static void 333 getFieldsGlobalsAndFuncs(const Decl &D, FieldSet &Fields, 334 llvm::DenseSet<const VarDecl *> &Vars, 335 llvm::DenseSet<const FunctionDecl *> &Funcs) { 336 insertIfGlobal(D, Vars); 337 insertIfFunction(D, Funcs); 338 if (const auto *Decomp = dyn_cast<DecompositionDecl>(&D)) 339 for (const auto *B : Decomp->bindings()) 340 if (auto *ME = dyn_cast_or_null<MemberExpr>(B->getBinding())) 341 // FIXME: should we be using `E->getFoundDecl()`? 342 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) 343 Fields.insert(FD); 344 } 345 346 /// Traverses `S` and inserts into `Fields`, `Vars` and `Funcs` any fields, 347 /// global variables and functions that are declared in or referenced from 348 /// sub-statements. 349 static void 350 getFieldsGlobalsAndFuncs(const Stmt &S, FieldSet &Fields, 351 llvm::DenseSet<const VarDecl *> &Vars, 352 llvm::DenseSet<const FunctionDecl *> &Funcs) { 353 for (auto *Child : S.children()) 354 if (Child != nullptr) 355 getFieldsGlobalsAndFuncs(*Child, Fields, Vars, Funcs); 356 if (const auto *DefaultInit = dyn_cast<CXXDefaultInitExpr>(&S)) 357 getFieldsGlobalsAndFuncs(*DefaultInit->getExpr(), Fields, Vars, Funcs); 358 359 if (auto *DS = dyn_cast<DeclStmt>(&S)) { 360 if (DS->isSingleDecl()) 361 getFieldsGlobalsAndFuncs(*DS->getSingleDecl(), Fields, Vars, Funcs); 362 else 363 for (auto *D : DS->getDeclGroup()) 364 getFieldsGlobalsAndFuncs(*D, Fields, Vars, Funcs); 365 } else if (auto *E = dyn_cast<DeclRefExpr>(&S)) { 366 insertIfGlobal(*E->getDecl(), Vars); 367 insertIfFunction(*E->getDecl(), Funcs); 368 } else if (const auto *C = dyn_cast<CXXMemberCallExpr>(&S)) { 369 // If this is a method that returns a member variable but does nothing else, 370 // model the field of the return value. 371 if (MemberExpr *E = getMemberForAccessor(*C)) 372 if (const auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) 373 Fields.insert(FD); 374 } else if (auto *E = dyn_cast<MemberExpr>(&S)) { 375 // FIXME: should we be using `E->getFoundDecl()`? 376 const ValueDecl *VD = E->getMemberDecl(); 377 insertIfGlobal(*VD, Vars); 378 insertIfFunction(*VD, Funcs); 379 if (const auto *FD = dyn_cast<FieldDecl>(VD)) 380 Fields.insert(FD); 381 } else if (auto *InitList = dyn_cast<InitListExpr>(&S)) { 382 if (InitList->getType()->isRecordType()) 383 for (const auto *FD : getFieldsForInitListExpr(InitList)) 384 Fields.insert(FD); 385 } 386 } 387 388 Environment::Environment(DataflowAnalysisContext &DACtx) 389 : DACtx(&DACtx), 390 FlowConditionToken(DACtx.arena().makeFlowConditionToken()) {} 391 392 Environment::Environment(DataflowAnalysisContext &DACtx, 393 const DeclContext &DeclCtx) 394 : Environment(DACtx) { 395 CallStack.push_back(&DeclCtx); 396 } 397 398 void Environment::initialize() { 399 const DeclContext *DeclCtx = getDeclCtx(); 400 if (DeclCtx == nullptr) 401 return; 402 403 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(DeclCtx)) { 404 assert(FuncDecl->doesThisDeclarationHaveABody()); 405 406 initFieldsGlobalsAndFuncs(FuncDecl); 407 408 for (const auto *ParamDecl : FuncDecl->parameters()) { 409 assert(ParamDecl != nullptr); 410 setStorageLocation(*ParamDecl, createObject(*ParamDecl, nullptr)); 411 } 412 } 413 414 if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(DeclCtx)) { 415 auto *Parent = MethodDecl->getParent(); 416 assert(Parent != nullptr); 417 418 if (Parent->isLambda()) { 419 for (auto Capture : Parent->captures()) { 420 if (Capture.capturesVariable()) { 421 const auto *VarDecl = Capture.getCapturedVar(); 422 assert(VarDecl != nullptr); 423 setStorageLocation(*VarDecl, createObject(*VarDecl, nullptr)); 424 } else if (Capture.capturesThis()) { 425 const auto *SurroundingMethodDecl = 426 cast<CXXMethodDecl>(DeclCtx->getNonClosureAncestor()); 427 QualType ThisPointeeType = 428 SurroundingMethodDecl->getFunctionObjectParameterType(); 429 setThisPointeeStorageLocation( 430 cast<RecordStorageLocation>(createObject(ThisPointeeType))); 431 } 432 } 433 } else if (MethodDecl->isImplicitObjectMemberFunction()) { 434 QualType ThisPointeeType = MethodDecl->getFunctionObjectParameterType(); 435 auto &ThisLoc = 436 cast<RecordStorageLocation>(createStorageLocation(ThisPointeeType)); 437 setThisPointeeStorageLocation(ThisLoc); 438 refreshRecordValue(ThisLoc, *this); 439 // Initialize fields of `*this` with values, but only if we're not 440 // analyzing a constructor; after all, it's the constructor's job to do 441 // this (and we want to be able to test that). 442 if (!isa<CXXConstructorDecl>(MethodDecl)) 443 initializeFieldsWithValues(ThisLoc); 444 } 445 } 446 } 447 448 // FIXME: Add support for resetting globals after function calls to enable 449 // the implementation of sound analyses. 450 void Environment::initFieldsGlobalsAndFuncs(const FunctionDecl *FuncDecl) { 451 assert(FuncDecl->doesThisDeclarationHaveABody()); 452 453 FieldSet Fields; 454 llvm::DenseSet<const VarDecl *> Vars; 455 llvm::DenseSet<const FunctionDecl *> Funcs; 456 457 // Look for global variable and field references in the 458 // constructor-initializers. 459 if (const auto *CtorDecl = dyn_cast<CXXConstructorDecl>(FuncDecl)) { 460 for (const auto *Init : CtorDecl->inits()) { 461 if (Init->isMemberInitializer()) { 462 Fields.insert(Init->getMember()); 463 } else if (Init->isIndirectMemberInitializer()) { 464 for (const auto *I : Init->getIndirectMember()->chain()) 465 Fields.insert(cast<FieldDecl>(I)); 466 } 467 const Expr *E = Init->getInit(); 468 assert(E != nullptr); 469 getFieldsGlobalsAndFuncs(*E, Fields, Vars, Funcs); 470 } 471 // Add all fields mentioned in default member initializers. 472 for (const FieldDecl *F : CtorDecl->getParent()->fields()) 473 if (const auto *I = F->getInClassInitializer()) 474 getFieldsGlobalsAndFuncs(*I, Fields, Vars, Funcs); 475 } 476 getFieldsGlobalsAndFuncs(*FuncDecl->getBody(), Fields, Vars, Funcs); 477 478 // These have to be added before the lines that follow to ensure that 479 // `create*` work correctly for structs. 480 DACtx->addModeledFields(Fields); 481 482 for (const VarDecl *D : Vars) { 483 if (getStorageLocation(*D) != nullptr) 484 continue; 485 486 setStorageLocation(*D, createObject(*D)); 487 } 488 489 for (const FunctionDecl *FD : Funcs) { 490 if (getStorageLocation(*FD) != nullptr) 491 continue; 492 auto &Loc = createStorageLocation(FD->getType()); 493 setStorageLocation(*FD, Loc); 494 } 495 } 496 497 Environment Environment::fork() const { 498 Environment Copy(*this); 499 Copy.FlowConditionToken = DACtx->forkFlowCondition(FlowConditionToken); 500 return Copy; 501 } 502 503 bool Environment::canDescend(unsigned MaxDepth, 504 const DeclContext *Callee) const { 505 return CallStack.size() <= MaxDepth && !llvm::is_contained(CallStack, Callee); 506 } 507 508 Environment Environment::pushCall(const CallExpr *Call) const { 509 Environment Env(*this); 510 511 if (const auto *MethodCall = dyn_cast<CXXMemberCallExpr>(Call)) { 512 if (const Expr *Arg = MethodCall->getImplicitObjectArgument()) { 513 if (!isa<CXXThisExpr>(Arg)) 514 Env.ThisPointeeLoc = 515 cast<RecordStorageLocation>(getStorageLocation(*Arg)); 516 // Otherwise (when the argument is `this`), retain the current 517 // environment's `ThisPointeeLoc`. 518 } 519 } 520 521 Env.pushCallInternal(Call->getDirectCallee(), 522 llvm::ArrayRef(Call->getArgs(), Call->getNumArgs())); 523 524 return Env; 525 } 526 527 Environment Environment::pushCall(const CXXConstructExpr *Call) const { 528 Environment Env(*this); 529 530 Env.ThisPointeeLoc = &Env.getResultObjectLocation(*Call); 531 532 Env.pushCallInternal(Call->getConstructor(), 533 llvm::ArrayRef(Call->getArgs(), Call->getNumArgs())); 534 535 return Env; 536 } 537 538 void Environment::pushCallInternal(const FunctionDecl *FuncDecl, 539 ArrayRef<const Expr *> Args) { 540 // Canonicalize to the definition of the function. This ensures that we're 541 // putting arguments into the same `ParamVarDecl`s` that the callee will later 542 // be retrieving them from. 543 assert(FuncDecl->getDefinition() != nullptr); 544 FuncDecl = FuncDecl->getDefinition(); 545 546 CallStack.push_back(FuncDecl); 547 548 initFieldsGlobalsAndFuncs(FuncDecl); 549 550 const auto *ParamIt = FuncDecl->param_begin(); 551 552 // FIXME: Parameters don't always map to arguments 1:1; examples include 553 // overloaded operators implemented as member functions, and parameter packs. 554 for (unsigned ArgIndex = 0; ArgIndex < Args.size(); ++ParamIt, ++ArgIndex) { 555 assert(ParamIt != FuncDecl->param_end()); 556 const VarDecl *Param = *ParamIt; 557 setStorageLocation(*Param, createObject(*Param, Args[ArgIndex])); 558 } 559 } 560 561 void Environment::popCall(const CallExpr *Call, const Environment &CalleeEnv) { 562 // We ignore some entries of `CalleeEnv`: 563 // - `DACtx` because is already the same in both 564 // - We don't want the callee's `DeclCtx`, `ReturnVal`, `ReturnLoc` or 565 // `ThisPointeeLoc` because they don't apply to us. 566 // - `DeclToLoc`, `ExprToLoc`, and `ExprToVal` capture information from the 567 // callee's local scope, so when popping that scope, we do not propagate 568 // the maps. 569 this->LocToVal = std::move(CalleeEnv.LocToVal); 570 this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken); 571 572 if (Call->isGLValue()) { 573 if (CalleeEnv.ReturnLoc != nullptr) 574 setStorageLocation(*Call, *CalleeEnv.ReturnLoc); 575 } else if (!Call->getType()->isVoidType()) { 576 if (CalleeEnv.ReturnVal != nullptr) 577 setValue(*Call, *CalleeEnv.ReturnVal); 578 } 579 } 580 581 void Environment::popCall(const CXXConstructExpr *Call, 582 const Environment &CalleeEnv) { 583 // See also comment in `popCall(const CallExpr *, const Environment &)` above. 584 this->LocToVal = std::move(CalleeEnv.LocToVal); 585 this->FlowConditionToken = std::move(CalleeEnv.FlowConditionToken); 586 587 if (Value *Val = CalleeEnv.getValue(*CalleeEnv.ThisPointeeLoc)) { 588 setValue(*Call, *Val); 589 } 590 } 591 592 bool Environment::equivalentTo(const Environment &Other, 593 Environment::ValueModel &Model) const { 594 assert(DACtx == Other.DACtx); 595 596 if (ReturnVal != Other.ReturnVal) 597 return false; 598 599 if (ReturnLoc != Other.ReturnLoc) 600 return false; 601 602 if (ThisPointeeLoc != Other.ThisPointeeLoc) 603 return false; 604 605 if (DeclToLoc != Other.DeclToLoc) 606 return false; 607 608 if (ExprToLoc != Other.ExprToLoc) 609 return false; 610 611 if (!compareKeyToValueMaps(ExprToVal, Other.ExprToVal, *this, Other, Model)) 612 return false; 613 614 if (!compareKeyToValueMaps(LocToVal, Other.LocToVal, *this, Other, Model)) 615 return false; 616 617 return true; 618 } 619 620 LatticeJoinEffect Environment::widen(const Environment &PrevEnv, 621 Environment::ValueModel &Model) { 622 assert(DACtx == PrevEnv.DACtx); 623 assert(ReturnVal == PrevEnv.ReturnVal); 624 assert(ReturnLoc == PrevEnv.ReturnLoc); 625 assert(ThisPointeeLoc == PrevEnv.ThisPointeeLoc); 626 assert(CallStack == PrevEnv.CallStack); 627 628 auto Effect = LatticeJoinEffect::Unchanged; 629 630 // By the API, `PrevEnv` is a previous version of the environment for the same 631 // block, so we have some guarantees about its shape. In particular, it will 632 // be the result of a join or widen operation on previous values for this 633 // block. For `DeclToLoc`, `ExprToVal`, and `ExprToLoc`, join guarantees that 634 // these maps are subsets of the maps in `PrevEnv`. So, as long as we maintain 635 // this property here, we don't need change their current values to widen. 636 assert(DeclToLoc.size() <= PrevEnv.DeclToLoc.size()); 637 assert(ExprToVal.size() <= PrevEnv.ExprToVal.size()); 638 assert(ExprToLoc.size() <= PrevEnv.ExprToLoc.size()); 639 640 ExprToVal = widenKeyToValueMap(ExprToVal, PrevEnv.ExprToVal, *this, PrevEnv, 641 Model, Effect); 642 643 LocToVal = widenKeyToValueMap(LocToVal, PrevEnv.LocToVal, *this, PrevEnv, 644 Model, Effect); 645 if (DeclToLoc.size() != PrevEnv.DeclToLoc.size() || 646 ExprToLoc.size() != PrevEnv.ExprToLoc.size() || 647 ExprToVal.size() != PrevEnv.ExprToVal.size() || 648 LocToVal.size() != PrevEnv.LocToVal.size()) 649 Effect = LatticeJoinEffect::Changed; 650 651 return Effect; 652 } 653 654 Environment Environment::join(const Environment &EnvA, const Environment &EnvB, 655 Environment::ValueModel &Model, 656 ExprJoinBehavior ExprBehavior) { 657 assert(EnvA.DACtx == EnvB.DACtx); 658 assert(EnvA.ThisPointeeLoc == EnvB.ThisPointeeLoc); 659 assert(EnvA.CallStack == EnvB.CallStack); 660 661 Environment JoinedEnv(*EnvA.DACtx); 662 663 JoinedEnv.CallStack = EnvA.CallStack; 664 JoinedEnv.ThisPointeeLoc = EnvA.ThisPointeeLoc; 665 666 if (EnvA.ReturnVal == nullptr || EnvB.ReturnVal == nullptr) { 667 // `ReturnVal` might not always get set -- for example if we have a return 668 // statement of the form `return some_other_func()` and we decide not to 669 // analyze `some_other_func()`. 670 // In this case, we can't say anything about the joined return value -- we 671 // don't simply want to propagate the return value that we do have, because 672 // it might not be the correct one. 673 // This occurs for example in the test `ContextSensitiveMutualRecursion`. 674 JoinedEnv.ReturnVal = nullptr; 675 } else if (areEquivalentValues(*EnvA.ReturnVal, *EnvB.ReturnVal)) { 676 JoinedEnv.ReturnVal = EnvA.ReturnVal; 677 } else { 678 assert(!EnvA.CallStack.empty()); 679 // FIXME: Make `CallStack` a vector of `FunctionDecl` so we don't need this 680 // cast. 681 auto *Func = dyn_cast<FunctionDecl>(EnvA.CallStack.back()); 682 assert(Func != nullptr); 683 if (Value *JoinedVal = 684 joinDistinctValues(Func->getReturnType(), *EnvA.ReturnVal, EnvA, 685 *EnvB.ReturnVal, EnvB, JoinedEnv, Model)) 686 JoinedEnv.ReturnVal = JoinedVal; 687 } 688 689 if (EnvA.ReturnLoc == EnvB.ReturnLoc) 690 JoinedEnv.ReturnLoc = EnvA.ReturnLoc; 691 else 692 JoinedEnv.ReturnLoc = nullptr; 693 694 JoinedEnv.DeclToLoc = intersectDeclToLoc(EnvA.DeclToLoc, EnvB.DeclToLoc); 695 696 // FIXME: update join to detect backedges and simplify the flow condition 697 // accordingly. 698 JoinedEnv.FlowConditionToken = EnvA.DACtx->joinFlowConditions( 699 EnvA.FlowConditionToken, EnvB.FlowConditionToken); 700 701 JoinedEnv.LocToVal = 702 joinLocToVal(EnvA.LocToVal, EnvB.LocToVal, EnvA, EnvB, JoinedEnv, Model); 703 704 if (ExprBehavior == KeepExprState) { 705 JoinedEnv.ExprToVal = joinExprMaps(EnvA.ExprToVal, EnvB.ExprToVal); 706 JoinedEnv.ExprToLoc = joinExprMaps(EnvA.ExprToLoc, EnvB.ExprToLoc); 707 } 708 709 return JoinedEnv; 710 } 711 712 StorageLocation &Environment::createStorageLocation(QualType Type) { 713 return DACtx->createStorageLocation(Type); 714 } 715 716 StorageLocation &Environment::createStorageLocation(const ValueDecl &D) { 717 // Evaluated declarations are always assigned the same storage locations to 718 // ensure that the environment stabilizes across loop iterations. Storage 719 // locations for evaluated declarations are stored in the analysis context. 720 return DACtx->getStableStorageLocation(D); 721 } 722 723 StorageLocation &Environment::createStorageLocation(const Expr &E) { 724 // Evaluated expressions are always assigned the same storage locations to 725 // ensure that the environment stabilizes across loop iterations. Storage 726 // locations for evaluated expressions are stored in the analysis context. 727 return DACtx->getStableStorageLocation(E); 728 } 729 730 void Environment::setStorageLocation(const ValueDecl &D, StorageLocation &Loc) { 731 assert(!DeclToLoc.contains(&D)); 732 DeclToLoc[&D] = &Loc; 733 } 734 735 StorageLocation *Environment::getStorageLocation(const ValueDecl &D) const { 736 auto It = DeclToLoc.find(&D); 737 if (It == DeclToLoc.end()) 738 return nullptr; 739 740 StorageLocation *Loc = It->second; 741 742 return Loc; 743 } 744 745 void Environment::removeDecl(const ValueDecl &D) { DeclToLoc.erase(&D); } 746 747 void Environment::setStorageLocation(const Expr &E, StorageLocation &Loc) { 748 // `DeclRefExpr`s to builtin function types aren't glvalues, for some reason, 749 // but we still want to be able to associate a `StorageLocation` with them, 750 // so allow these as an exception. 751 assert(E.isGLValue() || 752 E.getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)); 753 const Expr &CanonE = ignoreCFGOmittedNodes(E); 754 assert(!ExprToLoc.contains(&CanonE)); 755 ExprToLoc[&CanonE] = &Loc; 756 } 757 758 StorageLocation *Environment::getStorageLocation(const Expr &E) const { 759 // See comment in `setStorageLocation()`. 760 assert(E.isGLValue() || 761 E.getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)); 762 auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E)); 763 return It == ExprToLoc.end() ? nullptr : &*It->second; 764 } 765 766 // Returns whether a prvalue of record type is the one that originally 767 // constructs the object (i.e. it doesn't propagate it from one of its 768 // children). 769 static bool isOriginalRecordConstructor(const Expr &RecordPRValue) { 770 if (auto *Init = dyn_cast<InitListExpr>(&RecordPRValue)) 771 return !Init->isSemanticForm() || !Init->isTransparent(); 772 return isa<CXXConstructExpr>(RecordPRValue) || isa<CallExpr>(RecordPRValue) || 773 isa<LambdaExpr>(RecordPRValue) || 774 isa<CXXDefaultInitExpr>(RecordPRValue) || 775 // The framework currently does not propagate the objects created in 776 // the two branches of a `ConditionalOperator` because there is no way 777 // to reconcile their storage locations, which are different. We 778 // therefore claim that the `ConditionalOperator` is the expression 779 // that originally constructs the object. 780 // Ultimately, this will be fixed by propagating locations down from 781 // the result object, rather than up from the original constructor as 782 // we do now (see also the FIXME in the documentation for 783 // `getResultObjectLocation()`). 784 isa<ConditionalOperator>(RecordPRValue); 785 } 786 787 RecordStorageLocation & 788 Environment::getResultObjectLocation(const Expr &RecordPRValue) const { 789 assert(RecordPRValue.getType()->isRecordType()); 790 assert(RecordPRValue.isPRValue()); 791 792 // Returns a storage location that we can use if assertions fail. 793 auto FallbackForAssertFailure = 794 [this, &RecordPRValue]() -> RecordStorageLocation & { 795 return cast<RecordStorageLocation>( 796 DACtx->getStableStorageLocation(RecordPRValue)); 797 }; 798 799 if (isOriginalRecordConstructor(RecordPRValue)) { 800 auto *Val = cast_or_null<RecordValue>(getValue(RecordPRValue)); 801 // The builtin transfer function should have created a `RecordValue` for all 802 // original record constructors. 803 assert(Val); 804 if (!Val) 805 return FallbackForAssertFailure(); 806 return Val->getLoc(); 807 } 808 809 if (auto *Op = dyn_cast<BinaryOperator>(&RecordPRValue); 810 Op && Op->isCommaOp()) { 811 return getResultObjectLocation(*Op->getRHS()); 812 } 813 814 // All other expression nodes that propagate a record prvalue should have 815 // exactly one child. 816 llvm::SmallVector<const Stmt *> children(RecordPRValue.child_begin(), 817 RecordPRValue.child_end()); 818 assert(children.size() == 1); 819 if (children.empty()) 820 return FallbackForAssertFailure(); 821 822 return getResultObjectLocation(*cast<Expr>(children[0])); 823 } 824 825 PointerValue &Environment::getOrCreateNullPointerValue(QualType PointeeType) { 826 return DACtx->getOrCreateNullPointerValue(PointeeType); 827 } 828 829 void Environment::initializeFieldsWithValues(RecordStorageLocation &Loc) { 830 llvm::DenseSet<QualType> Visited; 831 int CreatedValuesCount = 0; 832 initializeFieldsWithValues(Loc, Visited, 0, CreatedValuesCount); 833 if (CreatedValuesCount > MaxCompositeValueSize) { 834 llvm::errs() << "Attempting to initialize a huge value of type: " 835 << Loc.getType() << '\n'; 836 } 837 } 838 839 void Environment::setValue(const StorageLocation &Loc, Value &Val) { 840 assert(!isa<RecordValue>(&Val) || &cast<RecordValue>(&Val)->getLoc() == &Loc); 841 842 LocToVal[&Loc] = &Val; 843 } 844 845 void Environment::setValue(const Expr &E, Value &Val) { 846 const Expr &CanonE = ignoreCFGOmittedNodes(E); 847 848 if (auto *RecordVal = dyn_cast<RecordValue>(&Val)) { 849 assert(isOriginalRecordConstructor(CanonE) || 850 &RecordVal->getLoc() == &getResultObjectLocation(CanonE)); 851 } 852 853 assert(CanonE.isPRValue()); 854 ExprToVal[&CanonE] = &Val; 855 } 856 857 Value *Environment::getValue(const StorageLocation &Loc) const { 858 return LocToVal.lookup(&Loc); 859 } 860 861 Value *Environment::getValue(const ValueDecl &D) const { 862 auto *Loc = getStorageLocation(D); 863 if (Loc == nullptr) 864 return nullptr; 865 return getValue(*Loc); 866 } 867 868 Value *Environment::getValue(const Expr &E) const { 869 if (E.isPRValue()) { 870 auto It = ExprToVal.find(&ignoreCFGOmittedNodes(E)); 871 return It == ExprToVal.end() ? nullptr : It->second; 872 } 873 874 auto It = ExprToLoc.find(&ignoreCFGOmittedNodes(E)); 875 if (It == ExprToLoc.end()) 876 return nullptr; 877 return getValue(*It->second); 878 } 879 880 Value *Environment::createValue(QualType Type) { 881 llvm::DenseSet<QualType> Visited; 882 int CreatedValuesCount = 0; 883 Value *Val = createValueUnlessSelfReferential(Type, Visited, /*Depth=*/0, 884 CreatedValuesCount); 885 if (CreatedValuesCount > MaxCompositeValueSize) { 886 llvm::errs() << "Attempting to initialize a huge value of type: " << Type 887 << '\n'; 888 } 889 return Val; 890 } 891 892 Value *Environment::createValueUnlessSelfReferential( 893 QualType Type, llvm::DenseSet<QualType> &Visited, int Depth, 894 int &CreatedValuesCount) { 895 assert(!Type.isNull()); 896 assert(!Type->isReferenceType()); 897 898 // Allow unlimited fields at depth 1; only cap at deeper nesting levels. 899 if ((Depth > 1 && CreatedValuesCount > MaxCompositeValueSize) || 900 Depth > MaxCompositeValueDepth) 901 return nullptr; 902 903 if (Type->isBooleanType()) { 904 CreatedValuesCount++; 905 return &makeAtomicBoolValue(); 906 } 907 908 if (Type->isIntegerType()) { 909 // FIXME: consider instead `return nullptr`, given that we do nothing useful 910 // with integers, and so distinguishing them serves no purpose, but could 911 // prevent convergence. 912 CreatedValuesCount++; 913 return &arena().create<IntegerValue>(); 914 } 915 916 if (Type->isPointerType()) { 917 CreatedValuesCount++; 918 QualType PointeeType = Type->getPointeeType(); 919 StorageLocation &PointeeLoc = 920 createLocAndMaybeValue(PointeeType, Visited, Depth, CreatedValuesCount); 921 922 return &arena().create<PointerValue>(PointeeLoc); 923 } 924 925 if (Type->isRecordType()) { 926 CreatedValuesCount++; 927 auto &Loc = cast<RecordStorageLocation>(createStorageLocation(Type)); 928 initializeFieldsWithValues(Loc, Visited, Depth, CreatedValuesCount); 929 930 return &refreshRecordValue(Loc, *this); 931 } 932 933 return nullptr; 934 } 935 936 StorageLocation & 937 Environment::createLocAndMaybeValue(QualType Ty, 938 llvm::DenseSet<QualType> &Visited, 939 int Depth, int &CreatedValuesCount) { 940 if (!Visited.insert(Ty.getCanonicalType()).second) 941 return createStorageLocation(Ty.getNonReferenceType()); 942 Value *Val = createValueUnlessSelfReferential( 943 Ty.getNonReferenceType(), Visited, Depth, CreatedValuesCount); 944 Visited.erase(Ty.getCanonicalType()); 945 946 Ty = Ty.getNonReferenceType(); 947 948 if (Val == nullptr) 949 return createStorageLocation(Ty); 950 951 if (Ty->isRecordType()) 952 return cast<RecordValue>(Val)->getLoc(); 953 954 StorageLocation &Loc = createStorageLocation(Ty); 955 setValue(Loc, *Val); 956 return Loc; 957 } 958 959 void Environment::initializeFieldsWithValues(RecordStorageLocation &Loc, 960 llvm::DenseSet<QualType> &Visited, 961 int Depth, 962 int &CreatedValuesCount) { 963 auto initField = [&](QualType FieldType, StorageLocation &FieldLoc) { 964 if (FieldType->isRecordType()) { 965 auto &FieldRecordLoc = cast<RecordStorageLocation>(FieldLoc); 966 setValue(FieldRecordLoc, create<RecordValue>(FieldRecordLoc)); 967 initializeFieldsWithValues(FieldRecordLoc, Visited, Depth + 1, 968 CreatedValuesCount); 969 } else { 970 if (!Visited.insert(FieldType.getCanonicalType()).second) 971 return; 972 if (Value *Val = createValueUnlessSelfReferential( 973 FieldType, Visited, Depth + 1, CreatedValuesCount)) 974 setValue(FieldLoc, *Val); 975 Visited.erase(FieldType.getCanonicalType()); 976 } 977 }; 978 979 for (const auto &[Field, FieldLoc] : Loc.children()) { 980 assert(Field != nullptr); 981 QualType FieldType = Field->getType(); 982 983 if (FieldType->isReferenceType()) { 984 Loc.setChild(*Field, 985 &createLocAndMaybeValue(FieldType, Visited, Depth + 1, 986 CreatedValuesCount)); 987 } else { 988 assert(FieldLoc != nullptr); 989 initField(FieldType, *FieldLoc); 990 } 991 } 992 for (const auto &[FieldName, FieldLoc] : Loc.synthetic_fields()) { 993 assert(FieldLoc != nullptr); 994 QualType FieldType = FieldLoc->getType(); 995 996 // Synthetic fields cannot have reference type, so we don't need to deal 997 // with this case. 998 assert(!FieldType->isReferenceType()); 999 initField(FieldType, Loc.getSyntheticField(FieldName)); 1000 } 1001 } 1002 1003 StorageLocation &Environment::createObjectInternal(const ValueDecl *D, 1004 QualType Ty, 1005 const Expr *InitExpr) { 1006 if (Ty->isReferenceType()) { 1007 // Although variables of reference type always need to be initialized, it 1008 // can happen that we can't see the initializer, so `InitExpr` may still 1009 // be null. 1010 if (InitExpr) { 1011 if (auto *InitExprLoc = getStorageLocation(*InitExpr)) 1012 return *InitExprLoc; 1013 } 1014 1015 // Even though we have an initializer, we might not get an 1016 // InitExprLoc, for example if the InitExpr is a CallExpr for which we 1017 // don't have a function body. In this case, we just invent a storage 1018 // location and value -- it's the best we can do. 1019 return createObjectInternal(D, Ty.getNonReferenceType(), nullptr); 1020 } 1021 1022 Value *Val = nullptr; 1023 if (InitExpr) { 1024 // In the (few) cases where an expression is intentionally 1025 // "uninterpreted", `InitExpr` is not associated with a value. There are 1026 // two ways to handle this situation: propagate the status, so that 1027 // uninterpreted initializers result in uninterpreted variables, or 1028 // provide a default value. We choose the latter so that later refinements 1029 // of the variable can be used for reasoning about the surrounding code. 1030 // For this reason, we let this case be handled by the `createValue()` 1031 // call below. 1032 // 1033 // FIXME. If and when we interpret all language cases, change this to 1034 // assert that `InitExpr` is interpreted, rather than supplying a 1035 // default value (assuming we don't update the environment API to return 1036 // references). 1037 Val = getValue(*InitExpr); 1038 1039 if (!Val && isa<ImplicitValueInitExpr>(InitExpr) && 1040 InitExpr->getType()->isPointerType()) 1041 Val = &getOrCreateNullPointerValue(InitExpr->getType()->getPointeeType()); 1042 } 1043 if (!Val) 1044 Val = createValue(Ty); 1045 1046 if (Ty->isRecordType()) 1047 return cast<RecordValue>(Val)->getLoc(); 1048 1049 StorageLocation &Loc = 1050 D ? createStorageLocation(*D) : createStorageLocation(Ty); 1051 1052 if (Val) 1053 setValue(Loc, *Val); 1054 1055 return Loc; 1056 } 1057 1058 void Environment::assume(const Formula &F) { 1059 DACtx->addFlowConditionConstraint(FlowConditionToken, F); 1060 } 1061 1062 bool Environment::proves(const Formula &F) const { 1063 return DACtx->flowConditionImplies(FlowConditionToken, F); 1064 } 1065 1066 bool Environment::allows(const Formula &F) const { 1067 return DACtx->flowConditionAllows(FlowConditionToken, F); 1068 } 1069 1070 void Environment::dump(raw_ostream &OS) const { 1071 llvm::DenseMap<const StorageLocation *, std::string> LocToName; 1072 if (ThisPointeeLoc != nullptr) 1073 LocToName[ThisPointeeLoc] = "this"; 1074 1075 OS << "DeclToLoc:\n"; 1076 for (auto [D, L] : DeclToLoc) { 1077 auto Iter = LocToName.insert({L, D->getNameAsString()}).first; 1078 OS << " [" << Iter->second << ", " << L << "]\n"; 1079 } 1080 OS << "ExprToLoc:\n"; 1081 for (auto [E, L] : ExprToLoc) 1082 OS << " [" << E << ", " << L << "]\n"; 1083 1084 OS << "ExprToVal:\n"; 1085 for (auto [E, V] : ExprToVal) 1086 OS << " [" << E << ", " << V << ": " << *V << "]\n"; 1087 1088 OS << "LocToVal:\n"; 1089 for (auto [L, V] : LocToVal) { 1090 OS << " [" << L; 1091 if (auto Iter = LocToName.find(L); Iter != LocToName.end()) 1092 OS << " (" << Iter->second << ")"; 1093 OS << ", " << V << ": " << *V << "]\n"; 1094 } 1095 1096 if (const FunctionDecl *Func = getCurrentFunc()) { 1097 if (Func->getReturnType()->isReferenceType()) { 1098 OS << "ReturnLoc: " << ReturnLoc; 1099 if (auto Iter = LocToName.find(ReturnLoc); Iter != LocToName.end()) 1100 OS << " (" << Iter->second << ")"; 1101 OS << "\n"; 1102 } else if (!Func->getReturnType()->isVoidType()) { 1103 if (ReturnVal == nullptr) 1104 OS << "ReturnVal: nullptr\n"; 1105 else 1106 OS << "ReturnVal: " << *ReturnVal << "\n"; 1107 } 1108 1109 if (isa<CXXMethodDecl>(Func)) { 1110 OS << "ThisPointeeLoc: " << ThisPointeeLoc << "\n"; 1111 } 1112 } 1113 1114 OS << "\n"; 1115 DACtx->dumpFlowCondition(FlowConditionToken, OS); 1116 } 1117 1118 void Environment::dump() const { 1119 dump(llvm::dbgs()); 1120 } 1121 1122 RecordStorageLocation *getImplicitObjectLocation(const CXXMemberCallExpr &MCE, 1123 const Environment &Env) { 1124 Expr *ImplicitObject = MCE.getImplicitObjectArgument(); 1125 if (ImplicitObject == nullptr) 1126 return nullptr; 1127 if (ImplicitObject->getType()->isPointerType()) { 1128 if (auto *Val = Env.get<PointerValue>(*ImplicitObject)) 1129 return &cast<RecordStorageLocation>(Val->getPointeeLoc()); 1130 return nullptr; 1131 } 1132 return cast_or_null<RecordStorageLocation>( 1133 Env.getStorageLocation(*ImplicitObject)); 1134 } 1135 1136 RecordStorageLocation *getBaseObjectLocation(const MemberExpr &ME, 1137 const Environment &Env) { 1138 Expr *Base = ME.getBase(); 1139 if (Base == nullptr) 1140 return nullptr; 1141 if (ME.isArrow()) { 1142 if (auto *Val = Env.get<PointerValue>(*Base)) 1143 return &cast<RecordStorageLocation>(Val->getPointeeLoc()); 1144 return nullptr; 1145 } 1146 return Env.get<RecordStorageLocation>(*Base); 1147 } 1148 1149 std::vector<const FieldDecl *> 1150 getFieldsForInitListExpr(const InitListExpr *InitList) { 1151 const RecordDecl *RD = InitList->getType()->getAsRecordDecl(); 1152 assert(RD != nullptr); 1153 1154 std::vector<const FieldDecl *> Fields; 1155 1156 if (InitList->getType()->isUnionType()) { 1157 Fields.push_back(InitList->getInitializedFieldInUnion()); 1158 return Fields; 1159 } 1160 1161 // Unnamed bitfields are only used for padding and do not appear in 1162 // `InitListExpr`'s inits. However, those fields do appear in `RecordDecl`'s 1163 // field list, and we thus need to remove them before mapping inits to 1164 // fields to avoid mapping inits to the wrongs fields. 1165 llvm::copy_if( 1166 RD->fields(), std::back_inserter(Fields), 1167 [](const FieldDecl *Field) { return !Field->isUnnamedBitfield(); }); 1168 return Fields; 1169 } 1170 1171 RecordValue &refreshRecordValue(RecordStorageLocation &Loc, Environment &Env) { 1172 auto &NewVal = Env.create<RecordValue>(Loc); 1173 Env.setValue(Loc, NewVal); 1174 return NewVal; 1175 } 1176 1177 RecordValue &refreshRecordValue(const Expr &Expr, Environment &Env) { 1178 assert(Expr.getType()->isRecordType()); 1179 1180 if (Expr.isPRValue()) { 1181 if (auto *ExistingVal = Env.get<RecordValue>(Expr)) { 1182 auto &NewVal = Env.create<RecordValue>(ExistingVal->getLoc()); 1183 Env.setValue(Expr, NewVal); 1184 Env.setValue(NewVal.getLoc(), NewVal); 1185 return NewVal; 1186 } 1187 1188 auto &NewVal = *cast<RecordValue>(Env.createValue(Expr.getType())); 1189 Env.setValue(Expr, NewVal); 1190 return NewVal; 1191 } 1192 1193 if (auto *Loc = Env.get<RecordStorageLocation>(Expr)) { 1194 auto &NewVal = Env.create<RecordValue>(*Loc); 1195 Env.setValue(*Loc, NewVal); 1196 return NewVal; 1197 } 1198 1199 auto &NewVal = *cast<RecordValue>(Env.createValue(Expr.getType())); 1200 Env.setStorageLocation(Expr, NewVal.getLoc()); 1201 return NewVal; 1202 } 1203 1204 } // namespace dataflow 1205 } // namespace clang 1206