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 namespace scope. Name can be a single
38// namespace (empty for anonymous namespace) or nested namespace.
40public:
42 emitNamespaceStarts(Name);
43 }
44
46
47 // Explicit function to close the namespace scopes.
48 void close() {
49 for (StringRef NS : llvm::reverse(Namespaces))
50 OS << "} // namespace " << NS << "\n";
51 Namespaces.clear();
52 }
53
54private:
55 void emitNamespaceStarts(StringRef Name) {
56 llvm::SplitString(Name, Namespaces, "::");
57 for (StringRef NS : Namespaces)
58 OS << "namespace " << NS << " {\n";
59 }
60
62 raw_ostream &OS;
63};
64
65} // end namespace llvm
66
67#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)
NamespaceEmitter(raw_ostream &OS, StringRef Name)
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.
LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters,...
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...