181ad6265SDimitry Andric //===- clang/Lex/DependencyDirectivesScanner.h ---------------------*- C++ -*-// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric /// 981ad6265SDimitry Andric /// \file 1081ad6265SDimitry Andric /// This is the interface for scanning header and source files to get the 1181ad6265SDimitry Andric /// minimum necessary preprocessor directives for evaluating includes. It 1281ad6265SDimitry Andric /// reduces the source down to #define, #include, #import, @import, and any 1381ad6265SDimitry Andric /// conditional preprocessor logic that contains one of those. 1481ad6265SDimitry Andric /// 1581ad6265SDimitry Andric //===----------------------------------------------------------------------===// 1681ad6265SDimitry Andric 1781ad6265SDimitry Andric #ifndef LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H 1881ad6265SDimitry Andric #define LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H 1981ad6265SDimitry Andric 2081ad6265SDimitry Andric #include "clang/Basic/SourceLocation.h" 2181ad6265SDimitry Andric #include "llvm/ADT/ArrayRef.h" 2281ad6265SDimitry Andric 2381ad6265SDimitry Andric namespace clang { 2481ad6265SDimitry Andric 2581ad6265SDimitry Andric namespace tok { 2681ad6265SDimitry Andric enum TokenKind : unsigned short; 2781ad6265SDimitry Andric } 2881ad6265SDimitry Andric 2981ad6265SDimitry Andric class DiagnosticsEngine; 3081ad6265SDimitry Andric 3181ad6265SDimitry Andric namespace dependency_directives_scan { 3281ad6265SDimitry Andric 3381ad6265SDimitry Andric /// Token lexed as part of dependency directive scanning. 3481ad6265SDimitry Andric struct Token { 3581ad6265SDimitry Andric /// Offset into the original source input. 3681ad6265SDimitry Andric unsigned Offset; 3781ad6265SDimitry Andric unsigned Length; 3881ad6265SDimitry Andric tok::TokenKind Kind; 3981ad6265SDimitry Andric unsigned short Flags; 4081ad6265SDimitry Andric TokenToken4181ad6265SDimitry Andric Token(unsigned Offset, unsigned Length, tok::TokenKind Kind, 4281ad6265SDimitry Andric unsigned short Flags) 4381ad6265SDimitry Andric : Offset(Offset), Length(Length), Kind(Kind), Flags(Flags) {} 4481ad6265SDimitry Andric getEndToken4581ad6265SDimitry Andric unsigned getEnd() const { return Offset + Length; } 4681ad6265SDimitry Andric isToken4781ad6265SDimitry Andric bool is(tok::TokenKind K) const { return Kind == K; } isNotToken4881ad6265SDimitry Andric bool isNot(tok::TokenKind K) const { return Kind != K; } isOneOfToken4981ad6265SDimitry Andric bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const { 5081ad6265SDimitry Andric return is(K1) || is(K2); 5181ad6265SDimitry Andric } isOneOfToken5281ad6265SDimitry Andric template <typename... Ts> bool isOneOf(tok::TokenKind K1, Ts... Ks) const { 5381ad6265SDimitry Andric return is(K1) || isOneOf(Ks...); 5481ad6265SDimitry Andric } 5581ad6265SDimitry Andric }; 5681ad6265SDimitry Andric 5781ad6265SDimitry Andric /// Represents the kind of preprocessor directive or a module declaration that 5881ad6265SDimitry Andric /// is tracked by the scanner in its token output. 5981ad6265SDimitry Andric enum DirectiveKind : uint8_t { 6081ad6265SDimitry Andric pp_none, 6181ad6265SDimitry Andric pp_include, 6281ad6265SDimitry Andric pp___include_macros, 6381ad6265SDimitry Andric pp_define, 6481ad6265SDimitry Andric pp_undef, 6581ad6265SDimitry Andric pp_import, 6681ad6265SDimitry Andric pp_pragma_import, 6781ad6265SDimitry Andric pp_pragma_once, 6881ad6265SDimitry Andric pp_pragma_push_macro, 6981ad6265SDimitry Andric pp_pragma_pop_macro, 7081ad6265SDimitry Andric pp_pragma_include_alias, 71*06c3fb27SDimitry Andric pp_pragma_system_header, 7281ad6265SDimitry Andric pp_include_next, 7381ad6265SDimitry Andric pp_if, 7481ad6265SDimitry Andric pp_ifdef, 7581ad6265SDimitry Andric pp_ifndef, 7681ad6265SDimitry Andric pp_elif, 7781ad6265SDimitry Andric pp_elifdef, 7881ad6265SDimitry Andric pp_elifndef, 7981ad6265SDimitry Andric pp_else, 8081ad6265SDimitry Andric pp_endif, 8181ad6265SDimitry Andric decl_at_import, 8281ad6265SDimitry Andric cxx_module_decl, 8381ad6265SDimitry Andric cxx_import_decl, 8481ad6265SDimitry Andric cxx_export_module_decl, 8581ad6265SDimitry Andric cxx_export_import_decl, 86bdd1243dSDimitry Andric /// Indicates that there are tokens present between the last scanned directive 87bdd1243dSDimitry Andric /// and eof. The \p Directive::Tokens array will be empty for this kind. 88bdd1243dSDimitry Andric tokens_present_before_eof, 8981ad6265SDimitry Andric pp_eof, 9081ad6265SDimitry Andric }; 9181ad6265SDimitry Andric 9281ad6265SDimitry Andric /// Represents a directive that's lexed as part of the dependency directives 9381ad6265SDimitry Andric /// scanning. It's used to track various preprocessor directives that could 94bdd1243dSDimitry Andric /// potentially have an effect on the dependencies. 9581ad6265SDimitry Andric struct Directive { 9681ad6265SDimitry Andric ArrayRef<Token> Tokens; 9781ad6265SDimitry Andric 9881ad6265SDimitry Andric /// The kind of token. 9981ad6265SDimitry Andric DirectiveKind Kind = pp_none; 10081ad6265SDimitry Andric 10181ad6265SDimitry Andric Directive() = default; DirectiveDirective10281ad6265SDimitry Andric Directive(DirectiveKind K, ArrayRef<Token> Tokens) 10381ad6265SDimitry Andric : Tokens(Tokens), Kind(K) {} 10481ad6265SDimitry Andric }; 10581ad6265SDimitry Andric 10681ad6265SDimitry Andric } // end namespace dependency_directives_scan 10781ad6265SDimitry Andric 10881ad6265SDimitry Andric /// Scan the input for the preprocessor directives that might have 10981ad6265SDimitry Andric /// an effect on the dependencies for a compilation unit. 11081ad6265SDimitry Andric /// 11181ad6265SDimitry Andric /// This function ignores all non-preprocessor code and anything that 11281ad6265SDimitry Andric /// can't affect what gets included. 11381ad6265SDimitry Andric /// 11481ad6265SDimitry Andric /// \returns false on success, true on error. If the diagnostic engine is not 11581ad6265SDimitry Andric /// null, an appropriate error is reported using the given input location 11681ad6265SDimitry Andric /// with the offset that corresponds to the \p Input buffer offset. 11781ad6265SDimitry Andric bool scanSourceForDependencyDirectives( 11881ad6265SDimitry Andric StringRef Input, SmallVectorImpl<dependency_directives_scan::Token> &Tokens, 11981ad6265SDimitry Andric SmallVectorImpl<dependency_directives_scan::Directive> &Directives, 12081ad6265SDimitry Andric DiagnosticsEngine *Diags = nullptr, 12181ad6265SDimitry Andric SourceLocation InputSourceLoc = SourceLocation()); 12281ad6265SDimitry Andric 12381ad6265SDimitry Andric /// Print the previously scanned dependency directives as minimized source text. 12481ad6265SDimitry Andric /// 12581ad6265SDimitry Andric /// \param Source The original source text that the dependency directives were 12681ad6265SDimitry Andric /// scanned from. 12781ad6265SDimitry Andric /// \param Directives The previously scanned dependency 12881ad6265SDimitry Andric /// directives. 12981ad6265SDimitry Andric /// \param OS the stream to print the dependency directives on. 13081ad6265SDimitry Andric /// 13181ad6265SDimitry Andric /// This is used primarily for testing purposes, during dependency scanning the 13281ad6265SDimitry Andric /// \p Lexer uses the tokens directly, not their printed version. 13381ad6265SDimitry Andric void printDependencyDirectivesAsSource( 13481ad6265SDimitry Andric StringRef Source, 13581ad6265SDimitry Andric ArrayRef<dependency_directives_scan::Directive> Directives, 13681ad6265SDimitry Andric llvm::raw_ostream &OS); 13781ad6265SDimitry Andric 13881ad6265SDimitry Andric } // end namespace clang 13981ad6265SDimitry Andric 14081ad6265SDimitry Andric #endif // LLVM_CLANG_LEX_DEPENDENCYDIRECTIVESSCANNER_H 141