[PATCH] D145244: [clang-format] Add ability to trace tokens.

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 6 03:05:54 PST 2023


sammccall added a comment.

Oops, sorry for the noise.

I'd forgotten that we're really interested in *why* we reach the conclusion, not the conclusion itself - and that file/line are our proxy for "why" as writing/maintaining descriptions is a burden.



================
Comment at: clang/lib/Format/ContinuationIndenter.cpp:289
                                    Current.closesBlockOrBlockTypeList(Style))) {
+    DEBUG_FORMAT_TRACE_TOKEN(Current, "!canBreak");
     return false;
----------------
klimek wrote:
> sammccall wrote:
> > annotating all exit paths from this function and `mustBreak` seems much more verbose and fragile than wrapping the function (making this version private) and adding the annotations in the wrapper.
> How do we get the exact condition that triggered? The main trick here is the __FILE__:__LINE__ in the debug output.
Doh. There *is* an a way in recent clang/gcc/msvc, though I suspect you don't want to go down this path...

```
template <typename T>
struct LogReturn {
  T Value;
  #ifdef NDEBUG
  LogReturn(T Value) : Value(Value) {}
  #else
  const char *File;
  int Line;
  // default args are evaluated at callsite
  LogReturn(T Value, const char *File=__builtin_FILE(), int Line=__builtin_LINE()) : Value(Value), File(File), Line(Line) {
    dbgs() << ...;
  }
  #endif
}

LogReturn<bool> priv() { return true; }
bool pub() { return priv().Value; }
```

Dunno how to get the token without resorting to TLS though. More realistically you could define `trace(bool, Token&, const char*File=..., etc)` and `return trace(true, Tok);`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D145244/new/

https://reviews.llvm.org/D145244



More information about the cfe-commits mailing list