1 //===--- iwyu_lexer_utils.h - clang-lexer utilities for iwyu --------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef INCLUDE_WHAT_YOU_USE_IWYU_LEXER_UTILS_H_ 11 #define INCLUDE_WHAT_YOU_USE_IWYU_LEXER_UTILS_H_ 12 13 #include <string> // for string 14 15 #include "clang/Basic/SourceLocation.h" 16 17 namespace clang { 18 class SourceManager; 19 class Token; 20 } // namespace clang 21 22 namespace include_what_you_use { 23 24 using std::string; 25 26 // For a particular source line that source_location points to, 27 // returns true if the given text occurs on the line. 28 // (Case sensitive.) 29 bool LineHasText(clang::SourceLocation source_location, llvm::StringRef text); 30 31 // Interface to get character data from a SourceLocation. This allows 32 // tests to avoid constructing a SourceManager yet still allow iwyu to 33 // get the character data from SourceLocations. 34 class CharacterDataGetterInterface { 35 public: 36 virtual ~CharacterDataGetterInterface() = default; 37 virtual const char* GetCharacterData(clang::SourceLocation loc) const = 0; 38 }; 39 40 // Implementation of CharacterDataGetterInterface that uses a SourceManager. 41 class SourceManagerCharacterDataGetter : public CharacterDataGetterInterface { 42 public: 43 explicit SourceManagerCharacterDataGetter( 44 const clang::SourceManager& source_manager); 45 const char* GetCharacterData(clang::SourceLocation loc) const override; 46 47 private: 48 const clang::SourceManager& source_manager_; 49 }; 50 51 // Returns the source-code line from the current location until \n. 52 llvm::StringRef GetSourceTextUntilEndOfLine( 53 clang::SourceLocation start_loc, 54 const CharacterDataGetterInterface& data_getter); 55 56 // Returns the location right *after* the first occurrence of needle 57 // after start_loc, if any. (If none, returns an invalid source-loc.) 58 // start_loc must be a valid source location. 59 clang::SourceLocation GetLocationAfter( 60 clang::SourceLocation start_loc, const string& needle, 61 const CharacterDataGetterInterface& data_getter); 62 63 // Returns the include-name as written, including <>'s and ""'s. 64 // Resolved computed includes first, so given 65 // #define INC <stdio.h> 66 // #include INC 67 // If include_loc points to the second INC, we'll return '<stdio.h>'. 68 string GetIncludeNameAsWritten( 69 clang::SourceLocation include_loc, 70 const CharacterDataGetterInterface& data_getter); 71 72 // Get the text of a given token. 73 string GetTokenText(const clang::Token& token, 74 const CharacterDataGetterInterface& data_getter); 75 76 } // namespace include_what_you_use 77 78 #endif // INCLUDE_WHAT_YOU_USE_IWYU_LEXER_UTILS_H_ 79