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/clangbackend/source/sourcelocation.cpp | |
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/clangbackend/source/sourcelocation.cpp')
-rw-r--r-- | src/tools/clangbackend/source/sourcelocation.cpp | 79 |
1 files changed, 39 insertions, 40 deletions
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) |