| 1 |
//===- Error.hh - Implements a simple error pointing mechanism *- C++ ---*-===// |
| 2 |
// |
| 3 |
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
/// \file |
| 10 |
/// Implements a simple error pointing mechanism. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <clang/Frontend/TextDiagnostic.h> |
| 19 |
#include <clang/Basic/LangOptions.h> |
| 20 |
|
| 21 |
// For CLANG_VERSION_MAJOR |
| 22 |
#include <clang/Basic/Version.h> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
|
| 26 |
/* Creates a special DiagnosticOptions with forced ShowColors. */ |
| 27 |
class DiagnosticOptionsWithColor |
| 28 |
{ |
| 29 |
public: |
| 30 |
DiagnosticOptionsWithColor(void); |
| 31 |
|
| 32 |
inline DiagnosticOptions *Get_DiagnosticOptions(void) |
| 33 |
{ |
| 34 |
return DOpts; |
| 35 |
} |
| 36 |
|
| 37 |
/* Because clang changed the constructor of TextDiagnostics, we need this |
| 38 |
special method here that returns the correct type just for it according |
| 39 |
to clang's version. */ |
| 40 |
#if CLANG_VERSION_MAJOR >= 21 |
| 41 |
inline DiagnosticOptions &getDiagsOptsToEngine(void) |
| 42 |
{ |
| 43 |
return *DOpts; |
| 44 |
} |
| 45 |
#else |
| 46 |
inline DiagnosticOptions *getDiagsOptsToEngine(void) |
| 47 |
{ |
| 48 |
return DOpts; |
| 49 |
} |
| 50 |
#endif |
| 51 |
|
| 52 |
inline bool Is_Colored(void) |
| 53 |
{ |
| 54 |
return DOpts->ShowColors; |
| 55 |
} |
| 56 |
|
| 57 |
private: |
| 58 |
DiagnosticOptions *DOpts; |
| 59 |
}; |
| 60 |
|
| 61 |
/* Diagnostics class, used to wrap error and warning messages. */ |
| 62 |
class DiagsClass |
| 63 |
{ |
| 64 |
public: |
| 65 |
DiagsClass(void); |
| 66 |
|
| 67 |
void EmitMessage(const StringRef message, DiagnosticsEngine::Level level, |
| 68 |
const SourceRange &range, const SourceManager &SM); |
| 69 |
|
| 70 |
void EmitMessage(const StringRef message, DiagnosticsEngine::Level level); |
| 71 |
|
| 72 |
private: |
| 73 |
inline const std::string Append_CE(StringRef message) |
| 74 |
{ |
| 75 |
return "clang-extract: " + message.str(); |
| 76 |
} |
| 77 |
|
| 78 |
LangOptions LangOpts; |
| 79 |
DiagnosticOptionsWithColor DOpts; |
| 80 |
TextDiagnostic DiagsEngine; |
| 81 |
|
| 82 |
protected: |
| 83 |
static DiagsClass sDiag; |
| 84 |
|
| 85 |
public: |
| 86 |
DiagsClass(DiagsClass &o) = delete; |
| 87 |
void operator=(const DiagsClass &o) = delete; |
| 88 |
|
| 89 |
static inline DiagsClass &Get_Instance(void) |
| 90 |
{ |
| 91 |
return sDiag; |
| 92 |
} |
| 93 |
|
| 94 |
static inline bool Is_Colored(void) |
| 95 |
{ |
| 96 |
return Get_Instance().DOpts.Is_Colored(); |
| 97 |
} |
| 98 |
|
| 99 |
static inline void Emit_Message(const StringRef message, |
| 100 |
DiagnosticsEngine::Level level, |
| 101 |
const SourceRange &range, |
| 102 |
const SourceManager &sm) |
| 103 |
{ |
| 104 |
sDiag.EmitMessage(message, level, range, sm); |
| 105 |
} |
| 106 |
|
| 107 |
static inline void Emit_Message(const StringRef message, |
| 108 |
DiagnosticsEngine::Level level) |
| 109 |
{ |
| 110 |
sDiag.EmitMessage(message, level); |
| 111 |
} |
| 112 |
|
| 113 |
static inline void Emit_Error(const StringRef message, |
| 114 |
const SourceRange &range, |
| 115 |
const SourceManager &sm) |
| 116 |
{ |
| 117 |
Emit_Message(message, DiagnosticsEngine::Level::Error, range, sm); |
| 118 |
} |
| 119 |
|
| 120 |
static inline void Emit_Error(const StringRef message) |
| 121 |
{ |
| 122 |
Emit_Message(message, DiagnosticsEngine::Level::Error); |
| 123 |
} |
| 124 |
|
| 125 |
static inline void Emit_Warn(const StringRef message, |
| 126 |
const SourceRange &range, |
| 127 |
const SourceManager &sm) |
| 128 |
{ |
| 129 |
Emit_Message(message, DiagnosticsEngine::Level::Warning, range, sm); |
| 130 |
} |
| 131 |
|
| 132 |
static inline void Emit_Warn(const StringRef message) |
| 133 |
{ |
| 134 |
Emit_Message(message, DiagnosticsEngine::Level::Warning); |
| 135 |
} |
| 136 |
|
| 137 |
static inline void Emit_Note(const StringRef message, |
| 138 |
const SourceRange &range, |
| 139 |
const SourceManager &sm) |
| 140 |
{ |
| 141 |
Emit_Message(message, DiagnosticsEngine::Level::Note, range, sm); |
| 142 |
} |
| 143 |
|
| 144 |
static inline void Emit_Note(const StringRef message) |
| 145 |
{ |
| 146 |
Emit_Message(message, DiagnosticsEngine::Level::Note); |
| 147 |
} |
| 148 |
}; |