Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLEliminateEmptyStatements.cpp
Go to the documentation of this file.
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
17
18#include <algorithm>
19#include <iterator>
20#include <memory>
21#include <vector>
22
23namespace SkSL {
24
25class Expression;
26
27static void eliminate_empty_statements(SkSpan<std::unique_ptr<ProgramElement>> elements) {
28 class EmptyStatementEliminator : public ProgramWriter {
29 public:
30 bool visitExpressionPtr(std::unique_ptr<Expression>& expr) override {
31 // We don't need to look inside expressions at all.
32 return false;
33 }
34
35 bool visitStatementPtr(std::unique_ptr<Statement>& stmt) override {
36 // Work from the innermost blocks to the outermost.
37 INHERITED::visitStatementPtr(stmt);
38
39 if (stmt->is<Block>()) {
40 StatementArray& children = stmt->as<Block>().children();
41 auto iter = std::remove_if(children.begin(), children.end(),
42 [](std::unique_ptr<Statement>& stmt) {
43 return stmt->isEmpty();
44 });
45 children.resize(std::distance(children.begin(), iter));
46 }
47
48 // We always check the entire program.
49 return false;
50 }
51
53 };
54
55 for (std::unique_ptr<ProgramElement>& pe : elements) {
56 if (pe->is<FunctionDefinition>()) {
57 EmptyStatementEliminator visitor;
58 visitor.visitStatementPtr(pe->as<FunctionDefinition>().body());
59 }
60 }
61}
62
66
67} // namespace SkSL
#define INHERITED(method,...)
std::unique_ptr< Statement > & body()
void resize(size_t count)
Definition SkTArray.h:418
void EliminateEmptyStatements(Module &module)
static void eliminate_empty_statements(SkSpan< std::unique_ptr< ProgramElement > > elements)
std::vector< std::unique_ptr< ProgramElement > > fElements