Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/CodeGen/VarBypassDetector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- VarBypassDetector.h - Bypass jumps detector --------------*- 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
#include "VarBypassDetector.h"
10
11
#include "clang/AST/Decl.h"
12
#include "clang/AST/Expr.h"
13
#include "clang/AST/Stmt.h"
14
15
using namespace clang;
16
using namespace CodeGen;
17
18
/// Clear the object and pre-process for the given statement, usually function
19
/// body statement.
20
13.6k
void VarBypassDetector::Init(const Stmt *Body) {
21
13.6k
  FromScopes.clear();
22
13.6k
  ToScopes.clear();
23
13.6k
  Bypasses.clear();
24
13.6k
  Scopes = {{~0U, nullptr}};
25
13.6k
  unsigned ParentScope = 0;
26
13.6k
  AlwaysBypassed = !BuildScopeInformation(Body, ParentScope);
27
13.6k
  if (!AlwaysBypassed)
28
13.6k
    Detect();
29
13.6k
}
30
31
/// Build scope information for a declaration that is part of a DeclStmt.
32
/// Returns false if we failed to build scope information and can't tell for
33
/// which vars are being bypassed.
34
bool VarBypassDetector::BuildScopeInformation(const Decl *D,
35
4.33k
                                              unsigned &ParentScope) {
36
4.33k
  const VarDecl *VD = dyn_cast<VarDecl>(D);
37
4.33k
  if (VD && 
VD->hasLocalStorage()4.19k
) {
38
4.05k
    Scopes.push_back({ParentScope, VD});
39
4.05k
    ParentScope = Scopes.size() - 1;
40
4.05k
  }
41
4.33k
42
4.33k
  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
43
4.19k
    if (const Expr *Init = VD->getInit())
44
2.38k
      return BuildScopeInformation(Init, ParentScope);
45
1.94k
46
1.94k
  return true;
47
1.94k
}
48
49
/// Walk through the statements, adding any labels or gotos to
50
/// LabelAndGotoScopes and recursively walking the AST as needed.
51
/// Returns false if we failed to build scope information and can't tell for
52
/// which vars are being bypassed.
53
bool VarBypassDetector::BuildScopeInformation(const Stmt *S,
54
218k
                                              unsigned &origParentScope) {
55
218k
  // If this is a statement, rather than an expression, scopes within it don't
56
218k
  // propagate out into the enclosing scope. Otherwise we have to worry about
57
218k
  // block literals, which have the lifetime of their enclosing statement.
58
218k
  unsigned independentParentScope = origParentScope;
59
218k
  unsigned &ParentScope =
60
218k
      ((isa<Expr>(S) && 
!isa<StmtExpr>(S)183k
) ?
origParentScope183k
61
218k
                                           : 
independentParentScope34.5k
);
62
218k
63
218k
  unsigned StmtsToSkip = 0u;
64
218k
65
218k
  switch (S->getStmtClass()) {
66
3
  case Stmt::IndirectGotoStmtClass:
67
3
    return false;
68
0
69
89
  case Stmt::SwitchStmtClass:
70
89
    if (const Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
71
0
      if (!BuildScopeInformation(Init, ParentScope))
72
0
        return false;
73
0
      ++StmtsToSkip;
74
0
    }
75
89
    if (const VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
76
0
      if (!BuildScopeInformation(Var, ParentScope))
77
0
        return false;
78
0
      ++StmtsToSkip;
79
0
    }
80
89
    LLVM_FALLTHROUGH;
81
89
82
3.09k
  case Stmt::GotoStmtClass:
83
3.09k
    FromScopes.push_back({S, ParentScope});
84
3.09k
    break;
85
89
86
4.28k
  case Stmt::DeclStmtClass: {
87
4.28k
    const DeclStmt *DS = cast<DeclStmt>(S);
88
4.28k
    for (auto *I : DS->decls())
89
4.33k
      if (!BuildScopeInformation(I, origParentScope))
90
0
        return false;
91
4.28k
    return true;
92
4.28k
  }
93
4.28k
94
4.28k
  case Stmt::CaseStmtClass:
95
0
  case Stmt::DefaultStmtClass:
96
0
  case Stmt::LabelStmtClass:
97
0
    llvm_unreachable("the loop below handles labels and cases");
98
0
    break;
99
0
100
210k
  default:
101
210k
    break;
102
214k
  }
103
214k
104
214k
  for (const Stmt *SubStmt : S->children()) {
105
202k
    if (!SubStmt)
106
182
      continue;
107
202k
    if (StmtsToSkip) {
108
0
      --StmtsToSkip;
109
0
      continue;
110
0
    }
111
202k
112
202k
    // Cases, labels, and defaults aren't "scope parents".  It's also
113
202k
    // important to handle these iteratively instead of recursively in
114
202k
    // order to avoid blowing out the stack.
115
202k
    
while (202k
true) {
116
202k
      const Stmt *Next;
117
202k
      if (const SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
118
290
        Next = SC->getSubStmt();
119
202k
      else if (const LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
120
15
        Next = LS->getSubStmt();
121
202k
      else
122
202k
        break;
123
305
124
305
      ToScopes[SubStmt] = ParentScope;
125
305
      SubStmt = Next;
126
305
    }
127
202k
128
202k
    // Recursively walk the AST.
129
202k
    if (!BuildScopeInformation(SubStmt, ParentScope))
130
3
      return false;
131
202k
  }
132
214k
  
return true214k
;
133
214k
}
134
135
/// Checks each jump and stores each variable declaration they bypass.
136
13.6k
void VarBypassDetector::Detect() {
137
13.6k
  for (const auto &S : FromScopes) {
138
3.09k
    const Stmt *St = S.first;
139
3.09k
    unsigned from = S.second;
140
3.09k
    if (const GotoStmt *GS = dyn_cast<GotoStmt>(St)) {
141
3.01k
      if (const LabelStmt *LS = GS->getLabel()->getStmt())
142
3.01k
        Detect(from, ToScopes[LS]);
143
3.01k
    } else 
if (const SwitchStmt *89
SS89
= dyn_cast<SwitchStmt>(St)) {
144
379
      for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
145
290
           SC = SC->getNextSwitchCase()) {
146
290
        Detect(from, ToScopes[SC]);
147
290
      }
148
89
    } else {
149
0
      llvm_unreachable("goto or switch was expected");
150
0
    }
151
3.09k
  }
152
13.6k
}
153
154
/// Checks the jump and stores each variable declaration it bypasses.
155
3.30k
void VarBypassDetector::Detect(unsigned From, unsigned To) {
156
3.30k
  while (From != To) {
157
8
    if (From < To) {
158
2
      assert(Scopes[To].first < To);
159
2
      const auto &ScopeTo = Scopes[To];
160
2
      To = ScopeTo.first;
161
2
      Bypasses.insert(ScopeTo.second);
162
6
    } else {
163
6
      assert(Scopes[From].first < From);
164
6
      From = Scopes[From].first;
165
6
    }
166
8
  }
167
3.30k
}