clang 24.0.0git
StmtUtils.h
Go to the documentation of this file.
1//===--- StmtUtils.h - Shared statement emission queries ---------*- C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// This file holds the AST queries about statements that both classic CodeGen
11// and CIR CodeGen need while emitting statements.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_CODEGENUTILS_STMTUTILS_H
16#define LLVM_CLANG_CODEGENUTILS_STMTUTILS_H
17
18#include "clang/AST/Stmt.h"
21#include "llvm/ADT/STLFunctionalExtras.h"
22
23namespace clang {
24class ASTContext;
25class Expr;
26} // namespace clang
27
28namespace clang::CodeGenUtils {
29
30// [C++26][stmt.iter.general] (DR)
31// A trivially empty iteration statement is an iteration statement matching
32// one of the following forms:
33// - while ( expression ) ;
34// - while ( expression ) { }
35// - do ; while ( expression ) ;
36// - do { } while ( expression ) ;
37// - for ( init-statement expression(opt); ) ;
38// - for ( init-statement expression(opt); ) { }
39template <typename LoopStmt> bool hasEmptyLoopBody(const LoopStmt &S) {
40 if constexpr (std::is_same_v<LoopStmt, ForStmt>) {
41 if (S.getInc())
42 return false;
43 }
44 const Stmt *Body = S.getBody();
45 if (!Body || isa<NullStmt>(Body))
46 return true;
47 if (const auto *Compound = dyn_cast<CompoundStmt>(Body))
48 return Compound->body_empty();
49 return false;
50}
51
52/// Returns true if a loop must make progress, which means the mustprogress
53/// attribute can be added to it. \p HasEmptyBody indicates whether the loop
54/// is trivially empty (see hasEmptyLoopBody()).
55///
56/// A loop that is both trivially empty and has a constant-true controlling
57/// expression (e.g. `while (true) {}`) is a trivial infinite loop, which is
58/// exempt from the forward-progress guarantee: [C++26][stmt.iter.general]
59/// (DR). When such a loop is found, \p RemoveMustProgress is called so the
60/// caller can delete the 'mustprogress' attribute it may have
61/// speculatively added to the enclosing function.
62bool checkIfLoopMustProgress(const LangOptions &LangOpts,
63 const CodeGenOptions &CGOpts, ASTContext &Ctx,
64 const Expr *ControllingExpression,
65 bool HasEmptyBody,
66 llvm::function_ref<void()> RemoveMustProgress);
67
68} // namespace clang::CodeGenUtils
69
70#endif // LLVM_CLANG_CODEGENUTILS_STMTUTILS_H
Defines the clang::LangOptions interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:239
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
This represents one expression.
Definition Expr.h:113
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Stmt - This represents one statement.
Definition Stmt.h:85
bool checkIfLoopMustProgress(const LangOptions &LangOpts, const CodeGenOptions &CGOpts, ASTContext &Ctx, const Expr *ControllingExpression, bool HasEmptyBody, llvm::function_ref< void()> RemoveMustProgress)
Returns true if a loop must make progress, which means the mustprogress attribute can be added to it.
Definition StmtUtils.cpp:15
bool hasEmptyLoopBody(const LoopStmt &S)
Definition StmtUtils.h:39
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330