[clang] Fix clang crash when printing highlighted code in diagnostic (after #66514) (PR #80442)

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


================
@@ -1349,7 +1349,7 @@ void TextDiagnostic::emitSnippetAndCaret(
   // Prepare source highlighting information for the lines we're about to
   // emit, starting from the first line.
   std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
-      highlightLines(BufStart, Lines.first, Lines.second, PP, LangOpts,
+      highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts,
----------------
alexfh wrote:

I'll try to provide what I have so far, but I doubt it's feasible to create a good test case for this.

So here's what I have so far (requires GLib 2.78.3 in third_party/glib/):
```
$ cat q.c
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "q.h"
$ cat q.h
#ifndef __Q_H__
#define __Q_H__

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

G_INLINE_FUNC void q();

#endif
$ cat stl/math.h
#ifndef _STL_MATH_H_
#define _STL_MATH_H_

#include_next <math.h>

#endif
$ cat stl/string.h
#ifndef _STL_STRING_H_
#define _STL_STRING_H_

#include_next <string.h>

#endif
$ clang -iquote . -isystem stl  -fdiagnostics-show-option -fcolor-diagnostics  -Wall -Werror --target=aarch64-unknown-linux-gnu -mcpu=neoverse-n1  --sysroot=our/custom/libc -D_Q1 -D_Q2 -D_Q3 -D_Q4 -D_Q5 -D_Q6 -D_Q7 -D_Q8 -D_Q9 -D_Q10 -D_Q11 -D_Q12 -D_Q13  -D_Q14  -D_Q15 -D_Q16 -D_Q17 -D_Q18 -D_Q19 -c q.c -fsyntax-only

In file included from q.c:4:
./q.h:7:1: warning: Deprecated pre-processor symbol: replace with "static inline" [-W#pragma-messages]
    7 | G_INLINE_FUNC void q();
      | ^
./third_party/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)
      |                                       ^
./third_party/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)
      |                                              ^
./third_party/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))
      |   ^
./third_party/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
assert.h assertion failed at llvm-project/clang/lib/Frontend/TextDiagnostic.cpp:1157 in std::unique_ptr<llvm::SmallVector<TextDiagnostic::StyleRange>[]> highlightLines(StringR
ef, unsigned int, unsigned int, const Preprocessor *, const LangOptions &, bool, FileID, const SourceManager &): CheckPoint >= Buff->getBufferStart() && CheckPoint <= Buff->getBufferEnd()
*** Check failure stack trace: ***
    @     0x562b1f8b03a3  __assert_fail
    @     0x562b170c558e  clang::TextDiagnostic::emitSnippetAndCaret()
    @     0x562b170c7b02  clang::TextDiagnostic::emitCodeContext()
    @     0x562b1709bcbc  clang::DiagnosticRenderer::emitDiagnostic()
    @     0x562b1709ef73  clang::DiagnosticRenderer::emitSingleMacroExpansion()
    @     0x562b1709cf56  clang::DiagnosticRenderer::emitMacroExpansions()
    @     0x562b1709bd47  clang::DiagnosticRenderer::emitDiagnostic()
    @     0x562b170bc4bb  clang::TextDiagnosticPrinter::HandleDiagnostic()
    @     0x562b1996038a  clang::DiagnosticIDs::EmitDiag()
    @     0x562b19960183  clang::DiagnosticIDs::ProcessDiag()
    @     0x562b199564ac  clang::DiagnosticsEngine::EmitCurrentDiagnostic()
    @     0x562b198aa621  (anonymous namespace)::PragmaMessageHandler::HandlePragma()
    @     0x562b198a12d6  clang::PragmaNamespace::HandlePragma()
    @     0x562b198a12d6  clang::PragmaNamespace::HandlePragma()
    @     0x562b198a1587  clang::Preprocessor::HandlePragmaDirective()
    @     0x562b198a21ad  clang::Preprocessor::Handle_Pragma()
    @     0x562b1988aec3  clang::Preprocessor::ExpandBuiltinMacro()
    @     0x562b19889599  clang::Preprocessor::HandleMacroExpandedIdentifier()
    @     0x562b198c32f8  clang::Preprocessor::HandleIdentifier()
    @     0x562b198d5023  clang::TokenLexer::Lex()
    @     0x562b1987c316  clang::Preprocessor::CLK_TokenLexer()
    @     0x562b198c3bfd  clang::Preprocessor::Lex()
    @     0x562b173942fa  clang::Parser::ConsumeToken()
    @     0x562b17393915  clang::Parser::ConsumeAnyToken()
    @     0x562b174784f5  clang::Parser::ParseDeclarationSpecifiers()
    @     0x562b1739ed03  clang::Parser::ParseDeclOrFunctionDefInternal()
    @     0x562b1739e4af  clang::Parser::ParseDeclarationOrFunctionDefinition()
    @     0x562b1739c864  clang::Parser::ParseExternalDeclaration()                                                                                                                                                                                      @     0x562b17399164  clang::Parser::ParseTopLevelDecl()
```

The problem is that I can't cut a reasonable subset of our libc without making the crash disappear.

I hope that's enough to understand the problem.

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


More information about the cfe-commits mailing list