| // Part of the Crubit project, under the Apache License v2.0 with LLVM | |
| // Exceptions. See /LICENSE for license information. | |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |
| #ifndef CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_ANNOTATIONS_H_ | |
| #define CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_ANNOTATIONS_H_ | |
| #include <memory> | |
| #include <string> | |
| #include "lifetime_annotations/function_lifetimes.h" | |
| #include "lifetime_annotations/lifetime_symbol_table.h" | |
| #include "clang/AST/Decl.h" | |
| #include "clang/Basic/SourceLocation.h" | |
| #include "clang/Frontend/CompilerInstance.h" | |
| #include "llvm/ADT/DenseSet.h" | |
| #include "llvm/Support/Error.h" | |
| namespace clang { | |
| namespace tidy { | |
| namespace lifetimes { | |
| // Context that is required to obtain lifetime annotations for a function. | |
| struct LifetimeAnnotationContext { | |
| // Files in which the `lifetime_elision` pragma was specified. | |
| llvm::DenseSet<clang::FileID> lifetime_elision_files; | |
| }; | |
| // Returns the lifetimes annotated on `func`. | |
| // If the file containing the function definition specifies the | |
| // `lifetime_elision` pragma, lifetime elision rules are used to determine | |
| // any unannotated lifetimes. | |
| // Returns an error if the function contains unannotated lifetimes that could | |
| // not be determined through lifetime elision, either because the | |
| // `lifetime_elision`pragma was not specified or because the lifetime elision | |
| // rules were not applicable. | |
| // The names of annotated function lifetimes as well as autogenerated names for | |
| // elided lifetimes are added to `symbol_table`. | |
| // | |
| // Returns structured error information as a `LifetimeError`. | |
| llvm::Expected<FunctionLifetimes> GetLifetimeAnnotations( | |
| const clang::FunctionDecl* func, const LifetimeAnnotationContext& context, | |
| LifetimeSymbolTable* symbol_table = nullptr); | |
| // Parses "a: b, a -> b"-style lifetime annotations from `lifetimes_str` for the | |
| // function declaration `func`. Lifetimes are inserted into the given | |
| // `symbol_table`, or used from there if already known. | |
| // | |
| // Returns structured error information as a `LifetimeError`. | |
| llvm::Expected<FunctionLifetimes> ParseLifetimeAnnotations( | |
| const clang::FunctionDecl* func, const std::string& lifetimes_str, | |
| LifetimeSymbolTable* symbol_table = nullptr); | |
| // Adds handlers to `preprocessor` to populate `context`. | |
| // To be able to use GetLifetimeAnnotations(), call this function to add the | |
| // necessary handlers before compiling any code. | |
| void AddLifetimeAnnotationHandlers( | |
| clang::Preprocessor& preprocessor, | |
| std::shared_ptr<LifetimeAnnotationContext> context); | |
| } // namespace lifetimes | |
| } // namespace tidy | |
| } // namespace clang | |
| #endif // CRUBIT_LIFETIME_ANNOTATIONS_LIFETIME_ANNOTATIONS_H_ |