Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/CodeGen/VarBypassDetector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- VarBypassDetector.cpp - 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
0
void VarBypassDetector::Init(const Stmt *Body) {
21
0
  FromScopes.clear();
22
0
  ToScopes.clear();
23
0
  Bypasses.clear();
24
0
  Scopes = {{~0U, nullptr}};
25
0
  unsigned ParentScope = 0;
26
0
  AlwaysBypassed = !BuildScopeInformation(Body, ParentScope);
27
0
  if (!AlwaysBypassed)
28
0
    Detect();
29
0
}
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
0
                                              unsigned &ParentScope) {
36
0
  const VarDecl *VD = dyn_cast<VarDecl>(D);
37
0
  if (VD && VD->hasLocalStorage()) {
38
0
    Scopes.push_back({ParentScope, VD});
39
0
    ParentScope = Scopes.size() - 1;
40
0
  }
41
42
0
  if (const VarDecl *VD = dyn_cast<VarDecl>(D))
43
0
    if (const Expr *Init = VD->getInit())
44
0
      return BuildScopeInformation(Init, ParentScope);
45
46
0
  return true;
47
0
}
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
0
                                              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
0
  unsigned independentParentScope = origParentScope;
59
0
  unsigned &ParentScope =
60
0
      ((isa<Expr>(S) && !isa<StmtExpr>(S)) ? origParentScope
61
0
                                           : independentParentScope);
62
63
0
  unsigned StmtsToSkip = 0u;
64
65
0
  switch (S->getStmtClass()) {
66
0
  case Stmt::IndirectGotoStmtClass:
67
0
    return false;
68
69
0
  case Stmt::SwitchStmtClass:
70
0
    if (const Stmt *Init = cast<SwitchStmt>(S)->getInit()) {
71
0
      if (!BuildScopeInformation(Init, ParentScope))
72
0
        return false;
73
0
      ++StmtsToSkip;
74
0
    }
75
0
    if (const VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
76
0
      if (!BuildScopeInformation(Var, ParentScope))
77
0
        return false;
78
0
      ++StmtsToSkip;
79
0
    }
80
0
    [[fallthrough]];
81
82
0
  case Stmt::GotoStmtClass:
83
0
    FromScopes.push_back({S, ParentScope});
84
0
    break;
85
86
0
  case Stmt::DeclStmtClass: {
87
0
    const DeclStmt *DS = cast<DeclStmt>(S);
88
0
    for (auto *I : DS->decls())
89
0
      if (!BuildScopeInformation(I, origParentScope))
90
0
        return false;
91
0
    return true;
92
0
  }
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
0
  default:
101
0
    break;
102
0
  }
103
104
0
  for (const Stmt *SubStmt : S->children()) {
105
0
    if (!SubStmt)
106
0
      continue;
107
0
    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
0
    while (true) {
116
0
      const Stmt *Next;
117
0
      if (const SwitchCase *SC = dyn_cast<SwitchCase>(SubStmt))
118
0
        Next = SC->getSubStmt();
119
0
      else if (const LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
120
0
        Next = LS->getSubStmt();
121
0
      else
122
0
        break;
123
124
0
      ToScopes[SubStmt] = ParentScope;
125
0
      SubStmt = Next;
126
0
    }
127
128
    // Recursively walk the AST.
129
0
    if (!BuildScopeInformation(SubStmt, ParentScope))
130
0
      return false;
131
0
  }
132
0
  return true;
133
0
}
134
135
/// Checks each jump and stores each variable declaration they bypass.
136
0
void VarBypassDetector::Detect() {
137
0
  for (const auto &S : FromScopes) {
138
0
    const Stmt *St = S.first;
139
0
    unsigned from = S.second;
140
0
    if (const GotoStmt *GS = dyn_cast<GotoStmt>(St)) {
141
0
      if (const LabelStmt *LS = GS->getLabel()->getStmt())
142
0
        Detect(from, ToScopes[LS]);
143
0
    } else if (const SwitchStmt *SS = dyn_cast<SwitchStmt>(St)) {
144
0
      for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
145
0
           SC = SC->getNextSwitchCase()) {
146
0
        Detect(from, ToScopes[SC]);
147
0
      }
148
0
    } else {
149
0
      llvm_unreachable("goto or switch was expected");
150
0
    }
151
0
  }
152
0
}
153
154
/// Checks the jump and stores each variable declaration it bypasses.
155
0
void VarBypassDetector::Detect(unsigned From, unsigned To) {
156
0
  while (From != To) {
157
0
    if (From < To) {
158
0
      assert(Scopes[To].first < To);
159
0
      const auto &ScopeTo = Scopes[To];
160
0
      To = ScopeTo.first;
161
0
      Bypasses.insert(ScopeTo.second);
162
0
    } else {
163
0
      assert(Scopes[From].first < From);
164
0
      From = Scopes[From].first;
165
0
    }
166
0
  }
167
0
}