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
38 // Explicit function to close the ifdef scopes.
39 void close() {
40 if (Closed)
41 return;
42
43 OS << "\n";
44 if (LateUndef)
45 OS << "#undef " << Name << "\n";
46 OS << "#endif // " << Name << "\n\n";
47 Closed = true;
48 }
49
50private:
51 std::string Name;
52 raw_ostream &OS;
53 bool LateUndef;
54 bool Closed = false;
55};
56
57// Simple RAII helper for emitting header include guard (ifndef-define-endif).
59public:
61 : Name(Name.str()), OS(OS) {
62 OS << "#ifndef " << Name << "\n"
63 << "#define " << Name << "\n\n";
64 }
66
67 // Explicit function to close the ifdef scopes.
68 void close() {
69 if (Closed)
70 return;
71 OS << "\n#endif // " << Name << "\n\n";
72 Closed = true;
73 }
74
75private:
76 std::string Name;
77 raw_ostream &OS;
78 bool Closed = false;
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
94 // Explicit function to close the namespace scopes.
95 void close() {
96 if (Closed)
97 return;
98 if (!Name.empty())
99 OS << "\n} // namespace " << Name << "\n";
100 Closed = true;
101 }
102
103private:
104 // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
105 // generated namespace scope needs to use
106 //
107 // namespace mlir::toy {
108 // }
109 //
110 // and cannot use "namespace ::mlir::toy".
111 static StringRef trim(StringRef Name) {
112 Name.consume_front("::");
113 return Name;
114 }
115 std::string Name;
116 raw_ostream &OS;
117 bool Closed = false;
118};
119
120} // end namespace llvm
121
122#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)
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.