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// Simple RAII helper for emitting ifdef-undef-endif scope.
25public:
26 IfDefEmitter(raw_ostream &OS, StringRef Name) : Name(Name.str()), OS(OS) {
27 OS << "#ifdef " << Name << "\n"
28 << "#undef " << Name << "\n\n";
29 }
30 ~IfDefEmitter() { OS << "\n#endif // " << Name << "\n\n"; }
31
32private:
33 std::string Name;
34 raw_ostream &OS;
35};
36
37// Simple RAII helper for emitting header include guard (ifndef-define-endif).
39public:
41 : Name(Name.str()), OS(OS) {
42 OS << "#ifndef " << Name << "\n"
43 << "#define " << Name << "\n\n";
44 }
45 ~IncludeGuardEmitter() { OS << "\n#endif // " << Name << "\n"; }
46
47private:
48 std::string Name;
49 raw_ostream &OS;
50};
51
52// Simple RAII helper for emitting namespace scope. Name can be a single
53// namespace or nested namespace. If the name is empty, will not generate any
54// namespace scope.
56public:
58 : Name(trim(NameUntrimmed).str()), OS(OS) {
59 if (!Name.empty())
60 OS << "namespace " << Name << " {\n";
61 }
62
64
65 // Explicit function to close the namespace scopes.
66 void close() {
67 if (!Closed && !Name.empty())
68 OS << "} // namespace " << Name << "\n";
69 Closed = true;
70 }
71
72private:
73 // Trim "::" prefix. If the namespace specified is ""::mlir::toy", then the
74 // generated namespace scope needs to use
75 //
76 // namespace mlir::toy {
77 // }
78 //
79 // and cannot use "namespace ::mlir::toy".
80 static StringRef trim(StringRef Name) {
81 Name.consume_front("::");
82 return Name;
83 }
84 std::string Name;
85 raw_ostream &OS;
86 bool Closed = false;
87};
88
89} // end namespace llvm
90
91#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)
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.