1//===- unittests/Driver/SimpleDiagnosticConsumer.h ------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Simple diagnostic consumer to grab up diagnostics for testing.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
14#define CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
15
16#include "clang/Driver/Driver.h"
17#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Support/VirtualFileSystem.h"
20
21struct 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(OutStr&: Errors.back());
27 } else {
28 Msgs.emplace_back();
29 Info.FormatDiagnostic(OutStr&: Msgs.back());
30 }
31 }
32 void clear() override {
33 Msgs.clear();
34 Errors.clear();
35 DiagnosticConsumer::clear();
36 }
37 std::vector<llvm::SmallString<32>> Msgs;
38 std::vector<llvm::SmallString<32>> Errors;
39};
40
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`.
44inline 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 clang::DiagnosticOptions DiagOpts;
51 clang::DiagnosticsEngine Diags(DiagID, DiagOpts, DiagConsumer);
52 return clang::driver::Driver("/bin/clang", "", Diags, "", InMemoryFileSystem);
53}
54
55#endif // CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
56

Provided by KDAB

Privacy Policy
Improve your Profiling and Debugging skills
Find out more

source code of clang/unittests/Driver/SimpleDiagnosticConsumer.h