xref: /llvm-project/clang/unittests/Driver/SimpleDiagnosticConsumer.h (revision 93857afc24abeeacdd58277b4ab32d38daa1e531)
10ac597f3SChris Bieneman //===- unittests/Driver/SimpleDiagnosticConsumer.h ------------------------===//
20ac597f3SChris Bieneman //
30ac597f3SChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40ac597f3SChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
50ac597f3SChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60ac597f3SChris Bieneman //
70ac597f3SChris Bieneman //===----------------------------------------------------------------------===//
80ac597f3SChris Bieneman //
90ac597f3SChris Bieneman // Simple diagnostic consumer to grab up diagnostics for testing.
100ac597f3SChris Bieneman //
110ac597f3SChris Bieneman //===----------------------------------------------------------------------===//
120ac597f3SChris Bieneman 
130ac597f3SChris Bieneman #ifndef CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
140ac597f3SChris Bieneman #define CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
150ac597f3SChris Bieneman 
16*93857afcSChristian Sigg #include "clang/Driver/Driver.h"
170ac597f3SChris Bieneman #include "clang/Basic/Diagnostic.h"
180ac597f3SChris Bieneman #include "llvm/ADT/SmallString.h"
1926bf0b4aSSimon Tatham #include "llvm/Support/VirtualFileSystem.h"
200ac597f3SChris Bieneman 
210ac597f3SChris Bieneman struct SimpleDiagnosticConsumer : public clang::DiagnosticConsumer {
220ac597f3SChris Bieneman   void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
230ac597f3SChris Bieneman                         const clang::Diagnostic &Info) override {
240ac597f3SChris Bieneman     if (DiagLevel == clang::DiagnosticsEngine::Level::Error) {
250ac597f3SChris Bieneman       Errors.emplace_back();
260ac597f3SChris Bieneman       Info.FormatDiagnostic(Errors.back());
270ac597f3SChris Bieneman     } else {
280ac597f3SChris Bieneman       Msgs.emplace_back();
290ac597f3SChris Bieneman       Info.FormatDiagnostic(Msgs.back());
300ac597f3SChris Bieneman     }
310ac597f3SChris Bieneman   }
320ac597f3SChris Bieneman   void clear() override {
330ac597f3SChris Bieneman     Msgs.clear();
340ac597f3SChris Bieneman     Errors.clear();
350ac597f3SChris Bieneman     DiagnosticConsumer::clear();
360ac597f3SChris Bieneman   }
370ac597f3SChris Bieneman   std::vector<llvm::SmallString<32>> Msgs;
380ac597f3SChris Bieneman   std::vector<llvm::SmallString<32>> Errors;
390ac597f3SChris Bieneman };
400ac597f3SChris Bieneman 
4126bf0b4aSSimon Tatham // Using SimpleDiagnosticConsumer, this function makes a clang Driver, suitable
4226bf0b4aSSimon Tatham // for testing situations where it will only ever be used for emitting
4326bf0b4aSSimon Tatham // diagnostics, such as being passed to `MultilibSet::select`.
4426bf0b4aSSimon Tatham inline clang::driver::Driver diagnostic_test_driver() {
4526bf0b4aSSimon Tatham   llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(
4626bf0b4aSSimon Tatham       new clang::DiagnosticIDs());
4726bf0b4aSSimon Tatham   llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
4826bf0b4aSSimon Tatham       new llvm::vfs::InMemoryFileSystem);
4926bf0b4aSSimon Tatham   auto *DiagConsumer = new SimpleDiagnosticConsumer;
5026bf0b4aSSimon Tatham   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> DiagOpts =
5126bf0b4aSSimon Tatham       new clang::DiagnosticOptions();
5226bf0b4aSSimon Tatham   clang::DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
5326bf0b4aSSimon Tatham   return clang::driver::Driver("/bin/clang", "", Diags, "", InMemoryFileSystem);
5426bf0b4aSSimon Tatham }
5526bf0b4aSSimon Tatham 
560ac597f3SChris Bieneman #endif // CLANG_UNITTESTS_SIMPLEDIAGNOSTICCONSUMER_H
57