|
7 | 7 | #include <clang/AST/RecursiveASTVisitor.h> |
8 | 8 | #include <clang/Sema/Sema.h> |
9 | 9 |
|
| 10 | +#include <array> |
10 | 11 | #include <functional> |
11 | 12 | #include <optional> |
12 | 13 | #include <stack> |
13 | 14 | #include <string> |
14 | 15 | #include <unordered_map> |
15 | 16 | #include <unordered_set> |
| 17 | +#include <utility> |
16 | 18 | #include <vector> |
17 | 19 |
|
18 | 20 | #include "converter/lex.h" |
@@ -315,7 +317,40 @@ class Converter : public clang::RecursiveASTVisitor<Converter> { |
315 | 317 | template <typename... Ts> |
316 | 318 | inline void _StrCat(const char *func, int line, const Ts &...vals) { |
317 | 319 | llvm::errs() << '[' << func << ':' << line << "] "; |
318 | | - ((llvm::errs() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...); |
| 320 | + ((llvm::errs() << vals << '\n', AppendToCode(*rs_code_, vals)), ...); |
| 321 | + } |
| 322 | + |
| 323 | + // Builds a std::array of N chars from the first N-1 chars of s followed by a |
| 324 | + // space. Used by AppendToCode to combine a string literal and its trailing |
| 325 | + // space into a single contiguous buffer for one append call. |
| 326 | + template <std::size_t N, std::size_t... I> |
| 327 | + static constexpr std::array<char, N> |
| 328 | + _MakeStringWithSpace(const char (&s)[N], std::index_sequence<I...>) { |
| 329 | + return {s[I]..., ' '}; |
| 330 | + } |
| 331 | + |
| 332 | + // General fallback: append val then a space (two operations). |
| 333 | + template <typename T> |
| 334 | + static void AppendToCode(std::string &rs_code, const T &val) { |
| 335 | + rs_code += val; |
| 336 | + rs_code += ' '; |
| 337 | + } |
| 338 | + |
| 339 | + // Specialization for string literals (const char[N]): |
| 340 | + // - N == 2 (single char, e.g. "("): use character append rather than string |
| 341 | + // append to avoid a strlen scan. |
| 342 | + // - N > 2 (longer string): combine the string and trailing space into one |
| 343 | + // contiguous buffer and append it in a single call. |
| 344 | + template <std::size_t N> |
| 345 | + static void AppendToCode(std::string &rs_code, const char (&val)[N]) { |
| 346 | + if constexpr (N == 2) { |
| 347 | + rs_code += val[0]; |
| 348 | + rs_code += ' '; |
| 349 | + } else { |
| 350 | + const auto combined = |
| 351 | + _MakeStringWithSpace(val, std::make_index_sequence<N - 1>{}); |
| 352 | + rs_code.append(combined.data(), N); |
| 353 | + } |
319 | 354 | } |
320 | 355 |
|
321 | 356 | class Buffer { |
|
0 commit comments