Debian Codegraph @

clang/unittests/Driver/SimpleDiagnosticConsumer.h

raw
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
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());
27 } else {
28 Msgs.emplace_back();
29 Info.FormatDiagnostic(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`.
44 inline clang::driver::Driver diagnostic_test_driver() {
45 auto InMemoryFileSystem =
46 llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
47 auto *DiagConsumer = new SimpleDiagnosticConsumer;
48 clang::DiagnosticOptions DiagOpts;
49 clang::DiagnosticsEngine Diags(clang::DiagnosticIDs::create(), DiagOpts,
50 DiagConsumer);
51 return clang::driver::Driver("/bin/clang", "", Diags, "", InMemoryFileSystem);
52 }
53
54 #endif // CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H