diff options
author | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-10-15 13:10:53 +0200 |
---|---|---|
committer | Ivan Donchevskii <ivan.donchevskii@qt.io> | 2018-10-17 05:56:39 +0000 |
commit | 78211741c882f347be2a4b141ba708a31a29912a (patch) | |
tree | 7df5360715934139afb63a2d099aa87f3104122e /src/tools | |
parent | 727ea78a5dd0ee8d0ce592aa9127373081eb3e15 (diff) | |
download | qt-creator-78211741c882f347be2a4b141ba708a31a29912a.tar.gz |
Clang: Make SourceLocation lazily calculate its fields
Trigger path, line, column and offset evaluation only if one
of them is accessed.
Change-Id: Ib2f8b06ece94a3b7424db28523b5796628865202
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src/tools')
6 files changed, 68 insertions, 59 deletions
diff --git a/src/tools/clangbackend/source/clangfollowsymbol.cpp b/src/tools/clangbackend/source/clangfollowsymbol.cpp index f8d763ae5e..27e8fb61e9 100644 --- a/src/tools/clangbackend/source/clangfollowsymbol.cpp +++ b/src/tools/clangbackend/source/clangfollowsymbol.cpp @@ -112,7 +112,7 @@ FollowSymbolResult FollowSymbol::followSymbol(CXTranslationUnit tu, if (cursor.kind() == CXCursor_InclusionDirective) { CXFile file = clang_getIncludedFile(cursors[tokenIndex].cx()); const ClangString filename(clang_getFileName(file)); - const SourceLocation loc(tu, filename, 1, 1); + const SourceLocation loc(tu, clang_getLocation(tu, file, 1, 1)); FollowSymbolResult result; result.range = SourceRangeContainer(SourceRange(loc, loc)); // CLANG-UPGRADE-CHECK: Remove if we don't use empty generated ui_* headers anymore. diff --git a/src/tools/clangbackend/source/clangtranslationunit.cpp b/src/tools/clangbackend/source/clangtranslationunit.cpp index 6eebc8e203..cc848f89ab 100644 --- a/src/tools/clangbackend/source/clangtranslationunit.cpp +++ b/src/tools/clangbackend/source/clangtranslationunit.cpp @@ -154,14 +154,22 @@ DiagnosticSet TranslationUnit::diagnostics() const SourceLocation TranslationUnit::sourceLocationAt(uint line,uint column) const { - return SourceLocation(m_cxTranslationUnit, m_filePath, line, column); + return SourceLocation(m_cxTranslationUnit, + clang_getLocation(m_cxTranslationUnit, + clang_getFile(m_cxTranslationUnit, + m_filePath.constData()), + line, column)); } SourceLocation TranslationUnit::sourceLocationAt(const Utf8String &filePath, uint line, uint column) const { - return SourceLocation(m_cxTranslationUnit, filePath, line, column); + return SourceLocation(m_cxTranslationUnit, + clang_getLocation(m_cxTranslationUnit, + clang_getFile(m_cxTranslationUnit, + filePath.constData()), + line, column)); } SourceRange TranslationUnit::sourceRange(uint fromLine, diff --git a/src/tools/clangbackend/source/skippedsourceranges.cpp b/src/tools/clangbackend/source/skippedsourceranges.cpp index f15bd44668..2ce2f15acb 100644 --- a/src/tools/clangbackend/source/skippedsourceranges.cpp +++ b/src/tools/clangbackend/source/skippedsourceranges.cpp @@ -66,7 +66,11 @@ static SourceRange adaptedSourceRange(CXTranslationUnit cxTranslationUnit, const return SourceRange { range.start(), - SourceLocation(cxTranslationUnit, end.filePath(), end.line(), 1) + SourceLocation(cxTranslationUnit, + clang_getLocation(cxTranslationUnit, + clang_getFile(cxTranslationUnit, + end.filePath().constData()), + end.line(), 1)) }; } diff --git a/src/tools/clangbackend/source/sourcelocation.cpp b/src/tools/clangbackend/source/sourcelocation.cpp index 965bd25129..70bb0e16aa 100644 --- a/src/tools/clangbackend/source/sourcelocation.cpp +++ b/src/tools/clangbackend/source/sourcelocation.cpp @@ -40,93 +40,92 @@ namespace ClangBackEnd { SourceLocation::SourceLocation() - : cxSourceLocation(clang_getNullLocation()) + : m_cxSourceLocation(clang_getNullLocation()) { } const Utf8String &SourceLocation::filePath() const { - if (isFilePathNormalized_) - return filePath_; + if (!m_isEvaluated) + evaluate(); - isFilePathNormalized_ = true; - filePath_ = FilePath::fromNativeSeparators(filePath_); + if (m_isFilePathNormalized) + return m_filePath; - return filePath_; + m_isFilePathNormalized = true; + m_filePath = FilePath::fromNativeSeparators(m_filePath); + + return m_filePath; } uint SourceLocation::line() const { - return line_; + if (!m_isEvaluated) + evaluate(); + return m_line; } uint SourceLocation::column() const { - return column_; + if (!m_isEvaluated) + evaluate(); + return m_column; } uint SourceLocation::offset() const { - return offset_; + if (!m_isEvaluated) + evaluate(); + return m_offset; } SourceLocationContainer SourceLocation::toSourceLocationContainer() const { - return SourceLocationContainer(filePath(), line_, column_); + if (!m_isEvaluated) + evaluate(); + return SourceLocationContainer(filePath(), m_line, m_column); } -SourceLocation::SourceLocation(CXTranslationUnit cxTranslationUnit, - CXSourceLocation cxSourceLocation) - : cxSourceLocation(cxSourceLocation) - , cxTranslationUnit(cxTranslationUnit) +void SourceLocation::evaluate() const { + m_isEvaluated = true; + CXFile cxFile; - clang_getFileLocation(cxSourceLocation, + clang_getFileLocation(m_cxSourceLocation, &cxFile, - &line_, - &column_, - &offset_); + &m_line, + &m_column, + &m_offset); - isFilePathNormalized_ = false; + m_isFilePathNormalized = false; if (!cxFile) return; - filePath_ = ClangString(clang_getFileName(cxFile)); - if (column_ > 1) { - const uint lineStart = offset_ + 1 - column_; - const char *contents = clang_getFileContents(cxTranslationUnit, cxFile, nullptr); + m_filePath = ClangString(clang_getFileName(cxFile)); + if (m_column > 1) { + const uint lineStart = m_offset + 1 - m_column; + const char *contents = clang_getFileContents(m_cxTranslationUnit, cxFile, nullptr); if (!contents) return; // (1) column in SourceLocation is the actual column shown by CppEditor. // (2) column in Clang is the utf8 byte offset from the beginning of the line. // Here we convert column from (2) to (1). - column_ = static_cast<uint>(QString::fromUtf8(&contents[lineStart], - static_cast<int>(column_) - 1).size()) + 1; + m_column = static_cast<uint>(QString::fromUtf8(&contents[lineStart], + static_cast<int>(m_column) - 1).size()) + 1; } } SourceLocation::SourceLocation(CXTranslationUnit cxTranslationUnit, - const Utf8String &filePath, - uint line, - uint column) - : cxSourceLocation(clang_getLocation(cxTranslationUnit, - clang_getFile(cxTranslationUnit, - filePath.constData()), - line, - column)), - cxTranslationUnit(cxTranslationUnit), - filePath_(filePath), - line_(line), - column_(column), - isFilePathNormalized_(true) + CXSourceLocation cxSourceLocation) + : m_cxSourceLocation(cxSourceLocation) + , m_cxTranslationUnit(cxTranslationUnit) { - clang_getFileLocation(cxSourceLocation, 0, 0, 0, &offset_); } SourceLocation::operator CXSourceLocation() const { - return cxSourceLocation; + return m_cxSourceLocation; } std::ostream &operator<<(std::ostream &os, const SourceLocation &sourceLocation) diff --git a/src/tools/clangbackend/source/sourcelocation.h b/src/tools/clangbackend/source/sourcelocation.h index da0e68f973..63a600a2a6 100644 --- a/src/tools/clangbackend/source/sourcelocation.h +++ b/src/tools/clangbackend/source/sourcelocation.h @@ -41,7 +41,7 @@ class SourceLocation friend bool operator==(const SourceLocation &first, const SourceLocation &second) { - return clang_equalLocations(first.cxSourceLocation, second.cxSourceLocation); + return clang_equalLocations(first.m_cxSourceLocation, second.m_cxSourceLocation); } friend bool operator!=(const SourceLocation &first, const SourceLocation &second) { @@ -51,10 +51,6 @@ class SourceLocation public: SourceLocation(); SourceLocation(CXTranslationUnit cxTranslationUnit, - const Utf8String &filePath, - uint line, - uint column); - SourceLocation(CXTranslationUnit cxTranslationUnit, CXSourceLocation cxSourceLocation); const Utf8String &filePath() const; @@ -64,20 +60,22 @@ public: SourceLocationContainer toSourceLocationContainer() const; - CXTranslationUnit tu() const { return cxTranslationUnit; } - CXSourceLocation cx() const { return cxSourceLocation; } + CXTranslationUnit tu() const { return m_cxTranslationUnit; } + CXSourceLocation cx() const { return m_cxSourceLocation; } private: operator CXSourceLocation() const; + void evaluate() const; private: - CXSourceLocation cxSourceLocation; - CXTranslationUnit cxTranslationUnit; - mutable Utf8String filePath_; - uint line_ = 0; - uint column_ = 0; - uint offset_ = 0; - mutable bool isFilePathNormalized_ = true; + CXSourceLocation m_cxSourceLocation; + CXTranslationUnit m_cxTranslationUnit; + mutable Utf8String m_filePath; + mutable uint m_line = 0; + mutable uint m_column = 0; + mutable uint m_offset = 0; + mutable bool m_isFilePathNormalized = true; + mutable bool m_isEvaluated = false; }; std::ostream &operator<<(std::ostream &os, const SourceLocation &sourceLocation); diff --git a/src/tools/clangbackend/source/sourcerange.cpp b/src/tools/clangbackend/source/sourcerange.cpp index e3a7da6445..413ddd77fa 100644 --- a/src/tools/clangbackend/source/sourcerange.cpp +++ b/src/tools/clangbackend/source/sourcerange.cpp @@ -40,7 +40,7 @@ SourceRange::SourceRange() SourceRange::SourceRange(const SourceLocation &start, const SourceLocation &end) : cxSourceRange(clang_getRange(start, end)), - cxTranslationUnit(start.cxTranslationUnit) + cxTranslationUnit(start.m_cxTranslationUnit) { } |