Skip to content

Commit ec3b1bb

Browse files
Copilotnunoplopes
andauthored
Thread llvm::raw_ostream through _AppendRuns; remove [[gnu::always_inline]]
- Remove [[gnu::always_inline]] from all _AppendRuns overloads and _ExtendBuf. Compile-time evaluation is now guaranteed by the template structure itself: BufSize and N are non-type template parameters deduced at each instantiation, and _ExtendBuf is constexpr, so the buffer construction is fully resolved by the template machinery. - Thread llvm::raw_ostream& dbg through every _AppendRuns overload. The debug stream now receives exactly the same data at exactly the same points as rs_code_, so the log line reflects what is actually appended rather than printing each raw argument piecemeal (one line per arg with a '\n'). - Each merged literal run is written to both rs_code_ and dbg in one shot (when the buffer is flushed). Each non-literal token is appended/printed as "val ' '" in both outputs. A single '\n' is written by _StrCat after the entire call chain completes. - Remove piecemeal ((errs() << vals << '\n'), ...) fold-expression from _StrCat; the debug output now comes exclusively from _AppendRuns. Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/9835d76c-3b8f-4869-ad66-5a66f6849cb4 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent a3f4805 commit ec3b1bb

1 file changed

Lines changed: 38 additions & 28 deletions

File tree

cpp2rust/converter/converter.h

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -322,24 +322,26 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
322322
// rs_code_ += val; rs_code_ += ' '; (two appends for the non-literal)
323323
// rs_code_ += "xyz " (one append for the trailing literal)
324324
//
325-
// All _AppendRuns overloads are marked [[gnu::always_inline]] so that Clang
326-
// inlines the entire recursive chain regardless of optimisation level. Once
327-
// inlined, the compiler can constant-fold the intermediate literal buffers
328-
// (which are built exclusively from compile-time data) into a single merged
329-
// string constant in the binary, giving true compile-time evaluation of the
330-
// buffer construction.
325+
// The debug stream receives exactly the same data at the same points, so the
326+
// log line reflects what is actually appended rather than piecemeal raw args.
327+
// Compile-time evaluation is achieved by template parameterisation: BufSize
328+
// and N are non-type template parameters deduced at each instantiation, and
329+
// _ExtendBuf is constexpr, so the buffer construction is resolved by the
330+
// template machinery without any compiler-specific inlining attributes.
331331
template <typename... Ts>
332332
inline void _StrCat(const char *func, int line, const Ts &...vals) {
333-
llvm::errs() << '[' << func << ':' << line << "] ";
334-
((llvm::errs() << vals << '\n'), ...);
335-
_AppendRuns(*rs_code_, std::array<char, 0>{}, vals...);
333+
llvm::raw_ostream &dbg = llvm::errs();
334+
dbg << '[' << func << ':' << line << "] ";
335+
_AppendRuns(dbg, *rs_code_, std::array<char, 0>{}, vals...);
336+
dbg << '\n';
336337
}
337338

338339
// Compile-time helper: extend a pending literal buffer with one more literal.
339-
// Marked constexpr so the compiler evaluates it at compile time whenever the
340-
// arguments are constant expressions (i.e. after inlining literal-head calls).
340+
// constexpr ensures the compiler evaluates this at compile time whenever the
341+
// arguments are constant expressions (i.e. once the template is instantiated
342+
// with the literal sizes known as non-type template parameters).
341343
template <std::size_t BufSize, std::size_t N>
342-
[[gnu::always_inline]] static constexpr std::array<char, BufSize + N>
344+
static constexpr std::array<char, BufSize + N>
343345
_ExtendBuf(std::array<char, BufSize> buf, const char (&val)[N]) {
344346
std::array<char, BufSize + N> new_buf{};
345347
std::copy(buf.begin(), buf.end(), new_buf.begin());
@@ -348,34 +350,42 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
348350
return new_buf;
349351
}
350352

351-
// Base case: no more arguments. Flush the pending literal buffer if any.
353+
// Base case: no more arguments. Flush the pending literal buffer (if any)
354+
// to both rs_code and the debug stream.
352355
template <std::size_t BufSize>
353-
[[gnu::always_inline]] static void
354-
_AppendRuns(std::string &rs_code, std::array<char, BufSize> buf) {
355-
if constexpr (BufSize > 0)
356+
static void _AppendRuns(llvm::raw_ostream &dbg, std::string &rs_code,
357+
std::array<char, BufSize> buf) {
358+
if constexpr (BufSize > 0) {
356359
rs_code.append(buf.data(), BufSize);
360+
dbg.write(buf.data(), BufSize);
361+
}
357362
}
358363

359-
// Head is a string literal: extend the pending buffer and recurse.
364+
// Head is a string literal: extend the pending buffer and recurse. The new
365+
// buffer size BufSize+N is a distinct template instantiation, so the compiler
366+
// knows the content dimensions entirely at compile time.
360367
template <std::size_t BufSize, std::size_t N, typename... Rest>
361-
[[gnu::always_inline]] static void
362-
_AppendRuns(std::string &rs_code, std::array<char, BufSize> buf,
363-
const char (&val)[N], const Rest &...rest) {
364-
_AppendRuns(rs_code, _ExtendBuf(buf, val), rest...);
368+
static void _AppendRuns(llvm::raw_ostream &dbg, std::string &rs_code,
369+
std::array<char, BufSize> buf,
370+
const char (&val)[N], const Rest &...rest) {
371+
_AppendRuns(dbg, rs_code, _ExtendBuf(buf, val), rest...);
365372
}
366373

367-
// Head is NOT a string literal: flush the pending buffer (if any), then
368-
// append the non-literal argument followed by a space (two separate appends),
374+
// Head is NOT a string literal: flush the pending buffer (if any) to both
375+
// outputs, then append/print the non-literal argument followed by a space,
369376
// and continue with an empty buffer.
370377
template <std::size_t BufSize, typename T, typename... Rest>
371-
[[gnu::always_inline]] static void
372-
_AppendRuns(std::string &rs_code, std::array<char, BufSize> buf,
373-
const T &val, const Rest &...rest) {
374-
if constexpr (BufSize > 0)
378+
static void _AppendRuns(llvm::raw_ostream &dbg, std::string &rs_code,
379+
std::array<char, BufSize> buf, const T &val,
380+
const Rest &...rest) {
381+
if constexpr (BufSize > 0) {
375382
rs_code.append(buf.data(), BufSize);
383+
dbg.write(buf.data(), BufSize);
384+
}
376385
rs_code += val;
377386
rs_code += ' ';
378-
_AppendRuns(rs_code, std::array<char, 0>{}, rest...);
387+
dbg << val << ' ';
388+
_AppendRuns(dbg, rs_code, std::array<char, 0>{}, rest...);
379389
}
380390

381391
class Buffer {

0 commit comments

Comments
 (0)