1*13fbcb42Sjoerg //===-- APINotesTest.cpp - API Notes Testing Tool ------------------ C++ --===//
2*13fbcb42Sjoerg //
3*13fbcb42Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*13fbcb42Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*13fbcb42Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*13fbcb42Sjoerg //
7*13fbcb42Sjoerg //===----------------------------------------------------------------------===//
8*13fbcb42Sjoerg 
9*13fbcb42Sjoerg #include "clang/APINotes/APINotesYAMLCompiler.h"
10*13fbcb42Sjoerg #include "llvm/Support/CommandLine.h"
11*13fbcb42Sjoerg #include "llvm/Support/FileSystem.h"
12*13fbcb42Sjoerg #include "llvm/Support/MemoryBuffer.h"
13*13fbcb42Sjoerg #include "llvm/Support/Signals.h"
14*13fbcb42Sjoerg #include "llvm/Support/ToolOutputFile.h"
15*13fbcb42Sjoerg #include "llvm/Support/WithColor.h"
16*13fbcb42Sjoerg 
17*13fbcb42Sjoerg static llvm::cl::list<std::string> APINotes(llvm::cl::Positional,
18*13fbcb42Sjoerg                                             llvm::cl::desc("[<apinotes> ...]"),
19*13fbcb42Sjoerg                                             llvm::cl::Required);
20*13fbcb42Sjoerg 
21*13fbcb42Sjoerg static llvm::cl::opt<std::string>
22*13fbcb42Sjoerg     OutputFileName("o", llvm::cl::desc("output filename"),
23*13fbcb42Sjoerg                    llvm::cl::value_desc("filename"), llvm::cl::init("-"));
24*13fbcb42Sjoerg 
main(int argc,const char ** argv)25*13fbcb42Sjoerg int main(int argc, const char **argv) {
26*13fbcb42Sjoerg   const bool DisableCrashReporting = true;
27*13fbcb42Sjoerg   llvm::sys::PrintStackTraceOnErrorSignal(argv[0], DisableCrashReporting);
28*13fbcb42Sjoerg   llvm::cl::ParseCommandLineOptions(argc, argv);
29*13fbcb42Sjoerg 
30*13fbcb42Sjoerg   auto Error = [](const llvm::Twine &Msg) {
31*13fbcb42Sjoerg     llvm::WithColor::error(llvm::errs(), "apinotes-test") << Msg << '\n';
32*13fbcb42Sjoerg   };
33*13fbcb42Sjoerg 
34*13fbcb42Sjoerg   std::error_code EC;
35*13fbcb42Sjoerg   auto Out = std::make_unique<llvm::ToolOutputFile>(OutputFileName, EC,
36*13fbcb42Sjoerg                                                     llvm::sys::fs::OF_None);
37*13fbcb42Sjoerg   if (EC) {
38*13fbcb42Sjoerg     Error("failed to open '" + OutputFileName + "': " + EC.message());
39*13fbcb42Sjoerg     return EXIT_FAILURE;
40*13fbcb42Sjoerg   }
41*13fbcb42Sjoerg 
42*13fbcb42Sjoerg   for (const std::string &Notes : APINotes) {
43*13fbcb42Sjoerg     llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> NotesOrError =
44*13fbcb42Sjoerg         llvm::MemoryBuffer::getFileOrSTDIN(Notes);
45*13fbcb42Sjoerg     if (std::error_code EC = NotesOrError.getError()) {
46*13fbcb42Sjoerg       llvm::errs() << EC.message() << '\n';
47*13fbcb42Sjoerg       return EXIT_FAILURE;
48*13fbcb42Sjoerg     }
49*13fbcb42Sjoerg 
50*13fbcb42Sjoerg     clang::api_notes::parseAndDumpAPINotes((*NotesOrError)->getBuffer(),
51*13fbcb42Sjoerg                                            Out->os());
52*13fbcb42Sjoerg   }
53*13fbcb42Sjoerg 
54*13fbcb42Sjoerg   return EXIT_SUCCESS;
55*13fbcb42Sjoerg }
56