[clang] [clang][Diagnostics] Highlight code snippets (PR #66514)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 2 02:55:07 PST 2024


bgra8 wrote:

https://github.com/llvm/llvm-project/pull/80023 does not fix the problem we're seeing here.

The problem is extremely hard to reduce as it happens during preprocessing and even removing unrelated headers causes the crash to disappear.

I've built `clang` with assertions enabled and debug information and got a bit of more information in the stack trace:

```
......
#6  0x00005555666ce7a4 in __assert_fail ()
#7  0x000055555e86b604 in highlightLines (FileData=..., StartLineNumber=40, EndLineNumber=40, PP=0x68f3f80ec98, LangOpts=..., ShowColors=true, FID=..., SM=...) at /work/llvm-project/clang/lib/Frontend/TextDiagnostic.cpp:1156
#8  0x000055555e86a7d3 in clang::TextDiagnostic::emitSnippetAndCaret (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Note, Ranges=..., Hints=...) at /work/llvm-project/clang/lib/Frontend/TextDiagnostic.cpp:1352
#9  0x000055555e86e908 in clang::TextDiagnostic::emitCodeContext (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Note, Ranges=..., Hints=...) at /work/llvm-project/clang/include/clang/Frontend/TextDiagnostic.h:97
#10 0x000055555e844c4e in clang::DiagnosticRenderer::emitCaret (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Note, Ranges=..., Hints=...) at /work/llvm-project/clang/lib/Frontend/DiagnosticRenderer.cpp:429
#11 0x000055555e844482 in clang::DiagnosticRenderer::emitDiagnostic (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Note, Message=..., Ranges=..., FixItHints=..., D=...) at /work/llvm-project/clang/lib/Frontend/DiagnosticRenderer.cpp:127
#12 0x000055555e84619f in clang::DiagnosticRenderer::emitSingleMacroExpansion (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Warning, Ranges=...) at /work/llvm-project/clang/lib/Frontend/DiagnosticRenderer.cpp:454
#13 0x000055555e845044 in clang::DiagnosticRenderer::emitMacroExpansions (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Warning, Ranges=..., Hints=...) at /work/llvm-project/clang/lib/Frontend/DiagnosticRenderer.cpp:569
#14 0x000055555e84454c in clang::DiagnosticRenderer::emitDiagnostic (this=0x68f3fc7cec0, Loc=..., Level=clang::DiagnosticsEngine::Warning, Message=..., Ranges=..., FixItHints=..., D=...) at /work/llvm-project/clang/lib/Frontend/DiagnosticRenderer.cpp:132
#15 0x000055555e86866f in clang::TextDiagnosticPrinter::HandleDiagnostic (this=0x68f3fc1c900, Level=clang::DiagnosticsEngine::Warning, Info=...) at /work/llvm-project/clang/lib/Frontend/TextDiagnosticPrinter.cpp:151
#16 0x0000555561435102 in clang::DiagnosticIDs::EmitDiag (this=0x68f3fc14bb0, Diag=..., DiagLevel=clang::DiagnosticIDs::Warning) at /work/llvm-project/clang/lib/Basic/DiagnosticIDs.cpp:823
#17 0x0000555561434f44 in clang::DiagnosticIDs::ProcessDiag (this=0x68f3fc14bb0, Diag=...) at /work/llvm-project/clang/lib/Basic/DiagnosticIDs.cpp:815
#18 0x0000555561429b49 in clang::DiagnosticsEngine::ProcessDiag (this=0x68f3f85c400) at /work/llvm-project/clang/include/clang/Basic/Diagnostic.h:1042
#19 0x0000555561429ac9 in clang::DiagnosticsEngine::EmitCurrentDiagnostic (this=0x68f3f85c400, Force=false) at /work/llvm-project/clang/lib/Basic/Diagnostic.cpp:545
.......
```

The failed assertion is in this block of code:

```
  const char *FirstLineStart =
      FileData.data() +
      SM.getDecomposedLoc(SM.translateLineCol(FID, StartLineNumber, 1)).second;
  if (const char *CheckPoint = PP->getCheckPoint(FID, FirstLineStart)) {
    // THIS IS THE FAILED ASSERTION:
    assert(CheckPoint >= Buff->getBufferStart() &&
           CheckPoint <= Buff->getBufferEnd());
    assert(CheckPoint <= FirstLineStart);
    size_t Offset = CheckPoint - Buff->getBufferStart();
    L.seek(Offset, /*IsAtStartOfLine=*/false);
  }
```

The crash happens when the preprocessor outputs a warning related to this code:

```
#ifndef __GTS_H__
#define __GTS_H__

#include <math.h>
#include <stdio.h>
// #include "glib.h"

#define G_STRINGIFY(macro_or_string)	G_STRINGIFY_ARG (macro_or_string)
#define	G_STRINGIFY_ARG(contents)	#contents
#define _GLIB_GNUC_DO_PRAGMA(x) _Pragma(G_STRINGIFY (x))
#define GLIB_DEPRECATED_MACRO_FOR(f) \
  _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Deprecated pre-processor symbol: replace with #f))
#define GLIB_DEPRECATED_MACRO_IN_2_48_FOR(f) GLIB_DEPRECATED_MACRO_FOR (f)
#define G_INLINE_FUNC static inline GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline)


/* Class declarations for base types */
G_INLINE_FUNC
gpointer         gts_object_is_from_class  (gpointer object,
					    gpointer klass);

#endif /* __GTS_H__ */
```

In the previous code snippet the macro definitions are extracted from the `lib.h` header (provided by package `libglib2.0-dev`). Just compiling that does not reproduce the crash but it might give you an idea of the code that might setup the crash condition. The compiler output just before the crash is:

```
In file included from matrix.c:1:
gts.h:9:1: warning: Deprecated pre-processor symbol: replace with "static inline" [-W#pragma-messages]
    9 | G_INLINE_FUNC
      | ^
glib/glib/gmacros.h:157:39: note: expanded from macro 'G_INLINE_FUNC'
  157 | #  define G_INLINE_FUNC static inline GLIB_DEPRECATED_MACRO_IN_2_48_FOR(static inline)
      |                                       ^
glib/glib/glib-visibility.h:414:46: note: expanded from macro 'GLIB_DEPRECATED_MACRO_IN_2_48_FOR'
  414 | #define GLIB_DEPRECATED_MACRO_IN_2_48_FOR(f) GLIB_DEPRECATED_MACRO_FOR (f)
      |                                              ^
glib/glib/gmacros.h:1299:3: note: expanded from macro 'GLIB_DEPRECATED_MACRO_FOR'
 1299 |   _GLIB_GNUC_DO_PRAGMA(GCC warning G_STRINGIFY (Deprecated pre-processor symbol: replace with #f))
      |   ^
glib/glib/gmacros.h:1296:33: note: expanded from macro '_GLIB_GNUC_DO_PRAGMA'
 1296 | #define _GLIB_GNUC_DO_PRAGMA(x) _Pragma(G_STRINGIFY (x))
      |                                 ^
<scratch space>:40:6: note: expanded from here
```

This crash is blocking our internal work at Google. I second @eaeltsin's proposal to put the  `highlightLines()` function behind a flag at least until we can get to the bottom of this. We can work together to further isolate the issue.

https://github.com/llvm/llvm-project/pull/66514


More information about the cfe-commits mailing list