Coverage Report

Created: 2020-09-22 08:39

/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
31.6k
void VarBypassDetector::Init(const Stmt *Body) {
21
31.6k
  FromScopes.clear();
22
31.6k
  ToScopes.clear();
23
31.6k
  Bypasses.clear();
24
31.6k
  Scopes = {{~0U, nullptr}};
25
31.6k
  unsigned ParentScope = 0;
26
31.6k
  AlwaysBypassed = !BuildScopeInformation(Body, ParentScope);
27
31.6k
  if (!AlwaysBypassed)
28
31.6k
    Detect();
29
31.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.90k
                                              unsigned &ParentScope) {
36
4.90k
  const VarDecl *VD = dyn_cast<VarDecl>(D);
37
4.90k
  if (VD && 
VD->hasLocalStorage()4.74k
) {
38
4.59k
    Scopes.push_back({ParentScope, VD});
39
4.59k
    ParentScope = Scopes.size() - 1;
40
4.59k
  }
41
42
4.90k
  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
43
4.74k
    if (const Expr *Init = VD->getInit())
44
2.70k
      return BuildScopeInformation(Init, ParentScope);
45
46
2.20k
  return true;
47
2.20k
}
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
405k
                                              unsigned &origParentScope) {
55
  // If this is a statement, rather than an expression, scopes within it don't
56
  // propagate out into the enclosing scope. Otherwise we have to worry about
57
  // block literals, which have the lifetime of their enclosing statement.
58
405k
  unsigned independentParentScope = origParentScope;
59
405k
  unsigned &ParentScope =
60
405k
      ((isa<Expr>(S) && 
!isa<StmtExpr>(S)334k
) ?
origParentScope334k
61
70.6k
                                           : independentParentScope);
62
63
405k
  unsigned StmtsToSkip = 0u;
64
65
405k
  switch (S->getStmtClass()) {
66
3
  case Stmt::IndirectGotoStmtClass:
67
3
    return false;
68
69
96
  case Stmt::SwitchStmtClass:
70
96
    if (const Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
71
0
      if (!BuildScopeInformation(Init, ParentScope))
72
0
        return false;
73
0
      ++StmtsToSkip;
74
0
    }
75
96
    if (const VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
76
0
      if (!BuildScopeInformation(Var, ParentScope))
77
0
        return false;
78
0
      ++StmtsToSkip;
79
0
    }
80
96
    LLVM_FALLTHROUGH;
81
82
3.10k
  case Stmt::GotoStmtClass:
83
3.10k
    FromScopes.push_back({S, ParentScope});
84
3.10k
    break;
85
86
4.83k
  case Stmt::DeclStmtClass: {
87
4.83k
    const DeclStmt *DS = cast<DeclStmt>(S);
88
4.83k
    for (auto *I : DS->decls())
89
4.90k
      if (!BuildScopeInformation(I, origParentScope))
90
0
        return false;
91
4.83k
    return true;
92
4.83k
  }
93
94
0
  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
100
397k
  default:
101
397k
    break;
102
400k
  }
103
104
400k
  for (const Stmt *SubStmt : S->children()) {
105
371k
    if (!SubStmt)
106
211
      continue;
107
370k
    if (StmtsToSkip) {
108
0
      --StmtsToSkip;
109
0
      continue;
110
0
    }
111
112
    // Cases, labels, and defaults aren't "scope parents".  It's also
113
    // important to handle these iteratively instead of recursively in
114
    // order to avoid blowing out the stack.
115
371k
    
while (370k
true) {
116
371k
      const Stmt *Next;
117
371k
      if (const SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
118
301
        Next = SC->getSubStmt();
119
371k
      else if (const LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
120
17
        Next = LS->getSubStmt();
121
370k
      else
122
370k
        break;
123
124
318
      ToScopes[SubStmt] = ParentScope;
125
318
      SubStmt = Next;
126
318
    }
127
128
    // Recursively walk the AST.
129
370k
    if (!BuildScopeInformation(SubStmt, ParentScope))
130
3
      return false;
131
370k
  }
132
400k
  return true;
133
400k
}
134
135
/// Checks each jump and stores each variable declaration they bypass.
136
31.6k
void VarBypassDetector::Detect() {
137
3.10k
  for (const auto &S : FromScopes) {
138
3.10k
    const Stmt *St = S.first;
139
3.10k
    unsigned from = S.second;
140
3.10k
    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
96
    } else if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(St)) {
144
397
      for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
145
301
           SC = SC->getNextSwitchCase()) {
146
301
        Detect(from, ToScopes[SC]);
147
301
      }
148
0
    } else {
149
0
      llvm_unreachable("goto or switch was expected");
150
0
    }
151
3.10k
  }
152
31.6k
}
153
154
/// Checks the jump and stores each variable declaration it bypasses.
155
3.31k
void VarBypassDetector::Detect(unsigned From, unsigned To) {
156
3.32k
  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.31k
}