/src/skia/src/sksl/transform/SkSLEliminateEmptyStatements.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2022 Google LLC |
3 | | * |
4 | | * Use of this source code is governed by a BSD-style license that can be |
5 | | * found in the LICENSE file. |
6 | | */ |
7 | | |
8 | | #include "include/core/SkSpan.h" |
9 | | #include "src/sksl/SkSLDefines.h" |
10 | | #include "src/sksl/SkSLModule.h" |
11 | | #include "src/sksl/ir/SkSLBlock.h" |
12 | | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
13 | | #include "src/sksl/ir/SkSLProgramElement.h" |
14 | | #include "src/sksl/ir/SkSLStatement.h" |
15 | | #include "src/sksl/transform/SkSLProgramWriter.h" |
16 | | #include "src/sksl/transform/SkSLTransform.h" |
17 | | |
18 | | #include <algorithm> |
19 | | #include <iterator> |
20 | | #include <memory> |
21 | | #include <vector> |
22 | | |
23 | | namespace SkSL { |
24 | | |
25 | | class Expression; |
26 | | |
27 | 0 | static void eliminate_empty_statements(SkSpan<std::unique_ptr<ProgramElement>> elements) { |
28 | 0 | class EmptyStatementEliminator : public ProgramWriter { |
29 | 0 | public: |
30 | 0 | bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override { |
31 | | // We don't need to look inside expressions at all. |
32 | 0 | return false; |
33 | 0 | } |
34 | |
|
35 | 0 | bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override { |
36 | | // Work from the innermost blocks to the outermost. |
37 | 0 | INHERITED::visitStatementPtr(stmt); |
38 | |
|
39 | 0 | if (stmt->is<Block>()) { |
40 | 0 | StatementArray& children = stmt->as<Block>().children(); |
41 | 0 | auto iter = std::remove_if(children.begin(), children.end(), |
42 | 0 | [](std::unique_ptr<Statement>& stmt) { |
43 | 0 | return stmt->isEmpty(); |
44 | 0 | }); |
45 | 0 | children.resize(std::distance(children.begin(), iter)); |
46 | 0 | } |
47 | | |
48 | | // We always check the entire program. |
49 | 0 | return false; |
50 | 0 | } |
51 | |
|
52 | 0 | using INHERITED = ProgramWriter; |
53 | 0 | }; |
54 | |
|
55 | 0 | for (std::unique_ptr<ProgramElement>& pe : elements) { |
56 | 0 | if (pe->is<FunctionDefinition>()) { |
57 | 0 | EmptyStatementEliminator visitor; |
58 | 0 | visitor.visitStatementPtr(pe->as<FunctionDefinition>().body()); |
59 | 0 | } |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | void Transform::EliminateEmptyStatements(Module& module) { |
64 | 0 | return eliminate_empty_statements(SkSpan(module.fElements)); |
65 | 0 | } |
66 | | |
67 | | } // namespace SkSL |