llvm-project/mlir/tools/mlir-tblgen/CppGenUtilities.cpp
Peng Chen 3138f6cc8c
[MLIR] Generate cpp comments for TableGen summary and description (#139606)
This commit takes the `summary` and `description` of TableGen files and
generate a cpp comments on top of the declarations of generated cpp
classes. 

The main motivation is to improve the developer experience. When people
work on compilers from an IDE, they will be able to hover over the
symbols (e.g. `"ADialect::BOp"`) in their cpp code and see the summary
and descriptions without having to referring to the `.td` files.
2025-05-14 22:09:48 +02:00

40 lines
1.2 KiB
C++

//===- CppGenUtilities.cpp - MLIR cpp gen utilities --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines common utilities for generating cpp files from tablegen
// structures.
//
//===----------------------------------------------------------------------===//
#include "CppGenUtilities.h"
#include "mlir/Support/IndentedOstream.h"
std::string
mlir::tblgen::emitSummaryAndDescComments(llvm::StringRef summary,
llvm::StringRef description) {
std::string comments = "";
StringRef trimmedSummary = summary.trim();
StringRef trimmedDesc = description.trim();
llvm::raw_string_ostream os(comments);
raw_indented_ostream ros(os);
if (!trimmedSummary.empty()) {
ros.printReindented(trimmedSummary, "/// ");
}
if (!trimmedDesc.empty()) {
if (!trimmedSummary.empty()) {
// If there is a summary, add a newline after it.
ros << "\n";
}
ros.printReindented(trimmedDesc, "/// ");
}
return comments;
}