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