LLVM 22.0.0git
CodeGenHelpers.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This file defines common utilities for generating C++ code.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TABLEGEN_CODEGENHELPERS_H
14#define LLVM_TABLEGEN_CODEGENHELPERS_H
15
16#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
20#include <string>
21
22namespace llvm {
23
24// Simple RAII helper for emitting ifdef-undef-endif scope. `LateUndef` controls
25// whether the undef is emitted at the start of the scope (false) or at the end
26// of the scope (true).
28public:
29 IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef = false)
30 : Name(Name.str()), OS(OS), LateUndef(LateUndef) {
31 OS << "#ifdef " << Name << "\n";
32 if (!LateUndef)
33 OS << "#undef " << Name << "\n";
34 OS << "\n";
35 }
37 OS << "\n";
38 if (LateUndef)
39 OS << "#undef " << Name << "\n";
40 OS << "#endif // " << Name << "\n\n";
41 }
42
43private:
44 std::string Name;
45 raw_ostream &OS;
46 bool LateUndef;
47};
48
49// Simple RAII helper for emitting #if guard. It emits:
50// #if <Condition>
51// #endif // <Condition>
53public:
55 : Condition(Condition.str()), OS(OS) {
56 OS << "#if " << Condition << "\n\n";
57 }
58
59 ~IfGuardEmitter() { OS << "\n#endif // " << Condition << "\n\n"; }
60
61private:
62 std::string Condition;
63 raw_ostream &OS;
64};
65
66// Simple RAII helper for emitting header include guard (ifndef-define-endif).
68public:
70 : Name(Name.str()), OS(OS) {
71 OS << "#ifndef " << Name << "\n"
72 << "#define " << Name << "\n\n";
73 }
74 ~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
75
76private:
77 std::string Name;
78 raw_ostream &OS;
79};
80
81// Simple RAII helper for emitting namespace scope. Name can be a single
82// namespace or nested namespace. If the name is empty, will not generate any
83// namespace scope.
85public:
87 : Name(trim(NameUntrimmed).str()), OS(OS) {
88 if (!Name.empty())
89 OS << "namespace " << Name << " {\n\n";
90 }
91
93 if (!Name.empty())
94 OS << "\n} // namespace " << Name << "\n";
95 }
96
97private:
98 // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
99 // generated namespace scope needs to use
100 //
101 // namespace mlir::toy {
102 // }
103 //
104 // and cannot use "namespace ::mlir::toy".
105 static StringRef trim(StringRef Name) {
106 Name.consume_front("::");
107 return Name;
108 }
109 std::string Name;
110 raw_ostream &OS;
111};
112
113} // end namespace llvm
114
115#endif // LLVM_TABLEGEN_CODEGENHELPERS_H
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
IfDefEmitter(raw_ostream &OS, StringRef Name, bool LateUndef=false)
IfGuardEmitter(raw_ostream &OS, StringRef Condition)
IncludeGuardEmitter(raw_ostream &OS, StringRef Name)
NamespaceEmitter(raw_ostream &OS, StringRef NameUntrimmed)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.