[PATCH] D147875: [clang][Diagnostics] Show line numbers when printing code snippets

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 5 09:08:34 PDT 2023


aaron.ballman added inline comments.


================
Comment at: clang/lib/Frontend/TextDiagnostic.cpp:1121-1138
+static unsigned getNumDisplayWidth(unsigned N) {
+  if (N < 10)
+    return 1;
+  if (N < 100)
+    return 2;
+  if (N < 1'000)
+    return 3;
----------------
jrtc27 wrote:
> kwk wrote:
> > This function screamed at me to be generalized so I gave it a try: https://gist.github.com/kwk/7e408065ea291e49fea4a83cf90a9cdf
> I don't think I want floating point arithmetic in my compiler... Something like:
> 
> ```
>     unsigned L, M;
>     for (L = 1U, M = 10U; N >= M && M != ~0U; ++L)
>         M = (M > ((~0U) / 10U)) ? (~0U) : (M * 10U);
>     
>     return (L);
> ```
> 
> is the generalised form (without all the redundant parentheses I added during debugging).
Cleaned up a bit:
```
constexpr unsigned getNumDisplayWidth(unsigned N) {
  unsigned L = 1U, M = 10U;
  constexpr unsigned Upper = ~0U / 10U;
  for (; N >= M && M != ~0U; ++L)
    M = M > Upper ? ~0U : M * 10U;
  return L;
}
```
https://godbolt.org/z/szTYsEM4v


================
Comment at: clang/test/FixIt/fixit-function-call.cpp:1
-// RUN: not %clang_cc1 -fdiagnostics-parseable-fixits -x c++ %s 2> %t
+// RUN: not %clang_cc1 -fdiagnostics-parseable-fixits -fno-diagnostics-show-line-numbers -fcaret-diagnostics-max-lines 1 -x c++ %s 2> %t
 // RUN: FileCheck %s < %t
----------------
tbaeder wrote:
> aaron.ballman wrote:
> > Just to double-check, parseable diagnostic output still works even if line numbers are shown, yes?
> Yes, at leat we don't prepend the line numbers to the parseable fixits:
> ```
>    21 | // CHECK-NEXT: fix-it{{.*}})
>    22 | // CHECK: void f1(double *a);
>    23 |   f1(a + 1);
>       |      ~~~~~
>       |      *(   )
> fix-it:"../clang/test/FixIt/fixit-function-call.cpp":{23:6-23:6}:"*("
> fix-it:"../clang/test/FixIt/fixit-function-call.cpp":{23:11-23:11}:")"
> ```
Excellent, thank you!


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

https://reviews.llvm.org/D147875



More information about the cfe-commits mailing list