1 //===- unittests/Driver/SimpleDiagnosticConsumer.h ------------------------===//
3 // Part of the LLVM Project, 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
7 //===----------------------------------------------------------------------===//
9 // Simple diagnostic consumer to grab up diagnostics for testing.
11 //===----------------------------------------------------------------------===//
13 #ifndef CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
14 #define CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
16 #include "clang/Driver/Driver.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/Support/VirtualFileSystem.h"
21 struct SimpleDiagnosticConsumer
: public clang::DiagnosticConsumer
{
22 void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel
,
23 const clang::Diagnostic
&Info
) override
{
24 if (DiagLevel
== clang::DiagnosticsEngine::Level::Error
) {
25 Errors
.emplace_back();
26 Info
.FormatDiagnostic(Errors
.back());
29 Info
.FormatDiagnostic(Msgs
.back());
32 void clear() override
{
35 DiagnosticConsumer::clear();
37 std::vector
<llvm::SmallString
<32>> Msgs
;
38 std::vector
<llvm::SmallString
<32>> Errors
;
41 // Using SimpleDiagnosticConsumer, this function makes a clang Driver, suitable
42 // for testing situations where it will only ever be used for emitting
43 // diagnostics, such as being passed to `MultilibSet::select`.
44 inline clang::driver::Driver
diagnostic_test_driver() {
45 llvm::IntrusiveRefCntPtr
<clang::DiagnosticIDs
> DiagID(
46 new clang::DiagnosticIDs());
47 llvm::IntrusiveRefCntPtr
<llvm::vfs::InMemoryFileSystem
> InMemoryFileSystem(
48 new llvm::vfs::InMemoryFileSystem
);
49 auto *DiagConsumer
= new SimpleDiagnosticConsumer
;
50 llvm::IntrusiveRefCntPtr
<clang::DiagnosticOptions
> DiagOpts
=
51 new clang::DiagnosticOptions();
52 clang::DiagnosticsEngine
Diags(DiagID
, &*DiagOpts
, DiagConsumer
);
53 return clang::driver::Driver("/bin/clang", "", Diags
, "", InMemoryFileSystem
);
56 #endif // CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H