clang  15.0.0git
TestAST.h
Go to the documentation of this file.
1 //===--- TestAST.h - Build clang ASTs for testing -------------------------===//
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 // In normal operation of Clang, the FrontendAction's lifecycle both creates
10 // and destroys the AST, and code should operate on it during callbacks in
11 // between (e.g. via ASTConsumer).
12 //
13 // For tests it is often more convenient to parse an AST from code, and keep it
14 // alive as a normal local object, with assertions as straight-line code.
15 // TestAST provides such an interface.
16 // (ASTUnit can be used for this purpose, but is a production library with
17 // broad scope and complicated API).
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_CLANG_TESTING_TESTAST_H
22 #define LLVM_CLANG_TESTING_TESTAST_H
23 
24 #include "clang/Basic/LLVM.h"
27 #include "llvm/ADT/StringRef.h"
28 #include <string>
29 #include <vector>
30 
31 namespace clang {
32 
33 /// Specifies a virtual source file to be parsed as part of a test.
34 struct TestInputs {
35  TestInputs() = default;
36  TestInputs(StringRef Code) : Code(Code) {}
37 
38  /// The source code of the input file to be parsed.
40 
41  /// The language to parse as.
42  /// This affects the -x and -std flags used, and the filename.
44 
45  /// Extra argv to pass to clang -cc1.
46  std::vector<std::string> ExtraArgs = {};
47 
48  /// Extra virtual files that are available to be #included.
49  /// Keys are plain filenames ("foo.h"), values are file content.
50  llvm::StringMap<std::string> ExtraFiles = {};
51 
52  /// By default, error diagnostics during parsing are reported as gtest errors.
53  /// To suppress this, set ErrorOK or include "error-ok" in a comment in Code.
54  /// In either case, all diagnostics appear in TestAST::diagnostics().
55  bool ErrorOK = false;
56 };
57 
58 /// The result of parsing a file specified by TestInputs.
59 ///
60 /// The ASTContext, Sema etc are valid as long as this object is alive.
61 class TestAST {
62 public:
63  /// Constructing a TestAST parses the virtual file.
64  ///
65  /// To keep tests terse, critical errors (e.g. invalid flags) are reported as
66  /// unit test failures with ADD_FAILURE() and produce an empty ASTContext,
67  /// Sema etc. This frees the test code from handling these explicitly.
68  TestAST(const TestInputs &);
69  TestAST(StringRef Code) : TestAST(TestInputs(Code)) {}
70  TestAST(TestAST &&M);
72  ~TestAST();
73 
74  /// Provides access to the AST context and other parts of Clang.
75 
76  ASTContext &context() { return Clang->getASTContext(); }
77  Sema &sema() { return Clang->getSema(); }
78  SourceManager &sourceManager() { return Clang->getSourceManager(); }
79  FileManager &fileManager() { return Clang->getFileManager(); }
80  Preprocessor &preprocessor() { return Clang->getPreprocessor(); }
81 
82  /// Returns diagnostics emitted during parsing.
83  /// (By default, errors cause test failures, see TestInputs::ErrorOK).
85 
86 private:
87  void clear();
88  std::unique_ptr<FrontendAction> Action;
89  std::unique_ptr<CompilerInstance> Clang;
90  std::vector<StoredDiagnostic> Diagnostics;
91 };
92 
93 } // end namespace clang
94 
95 #endif
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::TestAST::diagnostics
llvm::ArrayRef< StoredDiagnostic > diagnostics()
Returns diagnostics emitted during parsing.
Definition: TestAST.h:84
clang::TestInputs
Specifies a virtual source file to be parsed as part of a test.
Definition: TestAST.h:34
clang::TestAST
The result of parsing a file specified by TestInputs.
Definition: TestAST.h:61
clang::TestInputs::ExtraArgs
std::vector< std::string > ExtraArgs
Extra argv to pass to clang -cc1.
Definition: TestAST.h:46
clang::TestAST::TestAST
TestAST(const TestInputs &)
Constructing a TestAST parses the virtual file.
Definition: TestAST.cpp:75
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:627
clang::TestLanguage
TestLanguage
Definition: CommandLineArgs.h:23
clang::TestInputs::TestInputs
TestInputs(StringRef Code)
Definition: TestAST.h:36
clang::Lang_OBJCXX
@ Lang_OBJCXX
Definition: CommandLineArgs.h:33
clang::TestInputs::ErrorOK
bool ErrorOK
By default, error diagnostics during parsing are reported as gtest errors.
Definition: TestAST.h:55
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::TestAST::sourceManager
SourceManager & sourceManager()
Definition: TestAST.h:78
clang::TestAST::TestAST
TestAST(StringRef Code)
Definition: TestAST.h:69
clang::TestInputs::TestInputs
TestInputs()=default
clang::TestInputs::ExtraFiles
llvm::StringMap< std::string > ExtraFiles
Extra virtual files that are available to be #included.
Definition: TestAST.h:50
clang::Language
Language
The language for the input, used to select and validate the language standard and possible actions.
Definition: LangStandard.h:23
clang::TestAST::fileManager
FileManager & fileManager()
Definition: TestAST.h:79
llvm::ArrayRef
Definition: LLVM.h:34
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:354
LLVM.h
clang::TestAST::~TestAST
~TestAST()
Definition: TestAST.cpp:160
clang::TestAST::sema
Sema & sema()
Definition: TestAST.h:77
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::TestAST::preprocessor
Preprocessor & preprocessor()
Definition: TestAST.h:80
CommandLineArgs.h
clang::TestAST::operator=
TestAST & operator=(TestAST &&)
Definition: TestAST.cpp:150
CompilerInstance.h
clang::TestInputs::Code
std::string Code
The source code of the input file to be parsed.
Definition: TestAST.h:39
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::TestAST::context
ASTContext & context()
Provides access to the AST context and other parts of Clang.
Definition: TestAST.h:76