1 /* 2 * TranslationUnit.hpp 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 16 #ifndef CORE_LIBCLANG_TRANSLATION_UNIT_HPP 17 #define CORE_LIBCLANG_TRANSLATION_UNIT_HPP 18 19 #include <iosfwd> 20 #include <string> 21 22 #include <boost/shared_ptr.hpp> 23 24 #include "clang-c/Index.h" 25 26 #include "Cursor.hpp" 27 #include "Diagnostic.hpp" 28 #include "CodeCompleteResults.hpp" 29 #include "UnsavedFiles.hpp" 30 31 namespace rstudio { 32 namespace core { 33 namespace libclang { 34 35 class TranslationUnit 36 { 37 public: TranslationUnit()38 TranslationUnit() 39 : tu_(nullptr), pUnsavedFiles_(nullptr) 40 { 41 } 42 TranslationUnit(const std::string & filename,CXTranslationUnit tu,UnsavedFiles * pUnsavedFiles)43 TranslationUnit(const std::string& filename, 44 CXTranslationUnit tu, 45 UnsavedFiles* pUnsavedFiles) 46 : filename_(filename), tu_(tu), pUnsavedFiles_(pUnsavedFiles) 47 { 48 } 49 50 // translation units are managed and disposed by the SourceIndex, so 51 // so instances of this class can be freely copied 52 empty() const53 bool empty() const { return ! tu_; } 54 getCXTranslationUnit() const55 CXTranslationUnit getCXTranslationUnit() const { return tu_; } 56 57 std::string getSpelling() const; 58 59 bool includesFile(const std::string& filename) const; 60 61 CXFile getFile(const std::string& filename = std::string()) const; 62 63 CXResult findReferencesInFile(Cursor cursor, 64 CXCursorAndRangeVisitor visitor, 65 const std::string& filename = std::string()) const; 66 67 unsigned getNumDiagnostics() const; 68 boost::shared_ptr<Diagnostic> getDiagnostic(unsigned index) const; 69 70 // get the cursor for the translation unit 71 Cursor getCursor() const; 72 73 // NOTE: this can return a null cursor if no cursor is found 74 Cursor getCursor(const std::string& filename, 75 unsigned line, 76 unsigned column) const; 77 78 // NOTE: this can return an empty code completion object 79 // if the operation fails 80 boost::shared_ptr<CodeCompleteResults> codeCompleteAt( 81 const std::string& filename, 82 unsigned line, 83 unsigned column) const; 84 85 void printResourceUsage(std::ostream& ostr, bool detailed = false) const; 86 87 private: 88 std::string filename_; 89 CXTranslationUnit tu_; 90 UnsavedFiles* pUnsavedFiles_; 91 }; 92 93 } // namespace libclang 94 } // namespace core 95 } // namespace rstudio 96 97 98 #endif // CORE_LIBCLANG_TRANSLATION_UNIT_HPP 99