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