xref: /openbsd-src/gnu/llvm/clang/include/clang/Testing/TestAST.h (revision 12c855180aad702bbcca06e0398d774beeafb155)
1*12c85518Srobert //===--- TestAST.h - Build clang ASTs for testing -------------------------===//
2*12c85518Srobert //
3*12c85518Srobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*12c85518Srobert // See https://llvm.org/LICENSE.txt for license information.
5*12c85518Srobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*12c85518Srobert //
7*12c85518Srobert //===----------------------------------------------------------------------===//
8*12c85518Srobert //
9*12c85518Srobert // In normal operation of Clang, the FrontendAction's lifecycle both creates
10*12c85518Srobert // and destroys the AST, and code should operate on it during callbacks in
11*12c85518Srobert // between (e.g. via ASTConsumer).
12*12c85518Srobert //
13*12c85518Srobert // For tests it is often more convenient to parse an AST from code, and keep it
14*12c85518Srobert // alive as a normal local object, with assertions as straight-line code.
15*12c85518Srobert // TestAST provides such an interface.
16*12c85518Srobert // (ASTUnit can be used for this purpose, but is a production library with
17*12c85518Srobert // broad scope and complicated API).
18*12c85518Srobert //
19*12c85518Srobert //===----------------------------------------------------------------------===//
20*12c85518Srobert 
21*12c85518Srobert #ifndef LLVM_CLANG_TESTING_TESTAST_H
22*12c85518Srobert #define LLVM_CLANG_TESTING_TESTAST_H
23*12c85518Srobert 
24*12c85518Srobert #include "clang/Basic/LLVM.h"
25*12c85518Srobert #include "clang/Frontend/CompilerInstance.h"
26*12c85518Srobert #include "clang/Testing/CommandLineArgs.h"
27*12c85518Srobert #include "llvm/ADT/StringRef.h"
28*12c85518Srobert #include <string>
29*12c85518Srobert #include <vector>
30*12c85518Srobert 
31*12c85518Srobert namespace clang {
32*12c85518Srobert 
33*12c85518Srobert /// Specifies a virtual source file to be parsed as part of a test.
34*12c85518Srobert struct TestInputs {
35*12c85518Srobert   TestInputs() = default;
TestInputsTestInputs36*12c85518Srobert   TestInputs(StringRef Code) : Code(Code) {}
37*12c85518Srobert 
38*12c85518Srobert   /// The source code of the input file to be parsed.
39*12c85518Srobert   std::string Code;
40*12c85518Srobert 
41*12c85518Srobert   /// The language to parse as.
42*12c85518Srobert   /// This affects the -x and -std flags used, and the filename.
43*12c85518Srobert   TestLanguage Language = TestLanguage::Lang_OBJCXX;
44*12c85518Srobert 
45*12c85518Srobert   /// Extra argv to pass to clang -cc1.
46*12c85518Srobert   std::vector<std::string> ExtraArgs = {};
47*12c85518Srobert 
48*12c85518Srobert   /// Extra virtual files that are available to be #included.
49*12c85518Srobert   /// Keys are plain filenames ("foo.h"), values are file content.
50*12c85518Srobert   llvm::StringMap<std::string> ExtraFiles = {};
51*12c85518Srobert 
52*12c85518Srobert   /// By default, error diagnostics during parsing are reported as gtest errors.
53*12c85518Srobert   /// To suppress this, set ErrorOK or include "error-ok" in a comment in Code.
54*12c85518Srobert   /// In either case, all diagnostics appear in TestAST::diagnostics().
55*12c85518Srobert   bool ErrorOK = false;
56*12c85518Srobert 
57*12c85518Srobert   /// The action used to parse the code.
58*12c85518Srobert   /// By default, a SyntaxOnlyAction is used.
59*12c85518Srobert   std::function<std::unique_ptr<FrontendAction>()> MakeAction;
60*12c85518Srobert };
61*12c85518Srobert 
62*12c85518Srobert /// The result of parsing a file specified by TestInputs.
63*12c85518Srobert ///
64*12c85518Srobert /// The ASTContext, Sema etc are valid as long as this object is alive.
65*12c85518Srobert class TestAST {
66*12c85518Srobert public:
67*12c85518Srobert   /// Constructing a TestAST parses the virtual file.
68*12c85518Srobert   ///
69*12c85518Srobert   /// To keep tests terse, critical errors (e.g. invalid flags) are reported as
70*12c85518Srobert   /// unit test failures with ADD_FAILURE() and produce an empty ASTContext,
71*12c85518Srobert   /// Sema etc. This frees the test code from handling these explicitly.
72*12c85518Srobert   TestAST(const TestInputs &);
TestAST(StringRef Code)73*12c85518Srobert   TestAST(StringRef Code) : TestAST(TestInputs(Code)) {}
74*12c85518Srobert   TestAST(TestAST &&M);
75*12c85518Srobert   TestAST &operator=(TestAST &&);
76*12c85518Srobert   ~TestAST();
77*12c85518Srobert 
78*12c85518Srobert   /// Provides access to the AST context and other parts of Clang.
79*12c85518Srobert 
context()80*12c85518Srobert   ASTContext &context() { return Clang->getASTContext(); }
sema()81*12c85518Srobert   Sema &sema() { return Clang->getSema(); }
sourceManager()82*12c85518Srobert   SourceManager &sourceManager() { return Clang->getSourceManager(); }
fileManager()83*12c85518Srobert   FileManager &fileManager() { return Clang->getFileManager(); }
preprocessor()84*12c85518Srobert   Preprocessor &preprocessor() { return Clang->getPreprocessor(); }
action()85*12c85518Srobert   FrontendAction &action() { return *Action; }
86*12c85518Srobert 
87*12c85518Srobert   /// Returns diagnostics emitted during parsing.
88*12c85518Srobert   /// (By default, errors cause test failures, see TestInputs::ErrorOK).
diagnostics()89*12c85518Srobert   llvm::ArrayRef<StoredDiagnostic> diagnostics() { return Diagnostics; }
90*12c85518Srobert 
91*12c85518Srobert private:
92*12c85518Srobert   void clear();
93*12c85518Srobert   std::unique_ptr<FrontendAction> Action;
94*12c85518Srobert   std::unique_ptr<CompilerInstance> Clang;
95*12c85518Srobert   std::vector<StoredDiagnostic> Diagnostics;
96*12c85518Srobert };
97*12c85518Srobert 
98*12c85518Srobert } // end namespace clang
99*12c85518Srobert 
100*12c85518Srobert #endif
101