| 1 | //===- CppGenUtilities.cpp - MLIR cpp gen utilities --------------===// |
| 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 | // Defines common utilities for generating cpp files from tablegen |
| 10 | // structures. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CppGenUtilities.h" |
| 15 | #include "mlir/Support/IndentedOstream.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
| 19 | #include "llvm/TableGen/Error.h" |
| 20 | |
| 21 | void mlir::tblgen::emitSummaryAndDescComments(llvm::raw_ostream &os, |
| 22 | llvm::StringRef summary, |
| 23 | llvm::StringRef description, |
| 24 | bool ) { |
| 25 | StringRef trimmedSummary = summary.rtrim(); |
| 26 | StringRef trimmedDesc = description.rtrim(); |
| 27 | raw_indented_ostream ros(os); |
| 28 | |
| 29 | bool empty = true; |
| 30 | if (!trimmedSummary.empty()) { |
| 31 | ros.printReindented(str: trimmedSummary, extraPrefix: "/// " ); |
| 32 | empty = false; |
| 33 | } |
| 34 | |
| 35 | if (!trimmedDesc.empty()) { |
| 36 | if (!empty) { |
| 37 | // If there is a summary, add a newline after it. |
| 38 | ros << "\n" ; |
| 39 | } |
| 40 | ros.printReindented(str: trimmedDesc, extraPrefix: "/// " ); |
| 41 | empty = false; |
| 42 | } |
| 43 | |
| 44 | if (!empty && terminateComment) |
| 45 | ros << "\n" ; |
| 46 | } |
| 47 | |
| 48 | // Returns a description of what makes `header` unsafe to emit verbatim in |
| 49 | // `#include "<header>"`, or an empty string if it is safe. A header is unsafe |
| 50 | // if it could end the header name or the line, or start a comment. |
| 51 | static llvm::StringRef (llvm::StringRef ) { |
| 52 | if (header.contains(C: '"')) |
| 53 | return "'\"'" ; |
| 54 | if (header.contains(C: '\\')) |
| 55 | return "'\\'" ; |
| 56 | if (header.contains(Other: "/*" ) || header.contains(Other: "//" )) |
| 57 | return "a comment" ; |
| 58 | if (!llvm::all_of(Range&: header, P: [](char c) { return llvm::isPrint(C: c); })) |
| 59 | return "non-printable characters" ; |
| 60 | return "" ; |
| 61 | } |
| 62 | |
| 63 | void mlir::tblgen::emitIncludes(raw_ostream &os, ArrayRef<StringRef> ) { |
| 64 | SmallVector<StringRef> sorted(headers); |
| 65 | llvm::sort(C&: sorted); |
| 66 | sorted.erase(CS: llvm::unique(R&: sorted), CE: sorted.end()); |
| 67 | for (StringRef : sorted) { |
| 68 | StringRef reason = getUnsafeHeaderReason(header); |
| 69 | if (!reason.empty()) { |
| 70 | std::string escaped; |
| 71 | llvm::raw_string_ostream(escaped).write_escaped(Str: header); |
| 72 | llvm::PrintFatalError(Msg: "cannot emit '#include' for header \"" + escaped + |
| 73 | "\": header must not contain " + reason); |
| 74 | } |
| 75 | os << "#include \"" << header << "\"\n" ; |
| 76 | } |
| 77 | if (!sorted.empty()) |
| 78 | os << "\n" ; |
| 79 | } |
| 80 | |