summaryrefslogtreecommitdiff
path: root/src/qdoc/clangcodeparser.cpp
diff options
context:
space:
mode:
authorTopi Reinio <topi.reinio@qt.io>2020-04-14 15:30:36 +0200
committerTopi Reiniƶ <topi.reinio@qt.io>2020-04-21 06:04:31 +0000
commitd53af222939321fb816d44f86588c3eb8c255e10 (patch)
treec56b5853d743a516a1cb540ba5e8f3218c1eec56 /src/qdoc/clangcodeparser.cpp
parent8f379adf059977c04c2105b6ca73b04b1a923d8a (diff)
downloadqttools-d53af222939321fb816d44f86588c3eb8c255e10.tar.gz
qdoc: Fix handling of default values for function parameters
When using an \fn command to document a function that has default values for parameter(s), The Clang parser retrieves a relevant section of the source file that contains the expression for the default value. In case of \fn, the source is a temporary translation unit that was constructed on the fly - but it may not exist anymore, and the default value is lost. Fix this by storing the contents of the temporary TU for later access. Fixes: QTBUG-83472 Change-Id: I20ad9e3322636450db1def65933837c1c767f2c4 Reviewed-by: Paul Wicking <paul.wicking@qt.io>
Diffstat (limited to 'src/qdoc/clangcodeparser.cpp')
-rw-r--r--src/qdoc/clangcodeparser.cpp30
1 files changed, 19 insertions, 11 deletions
diff --git a/src/qdoc/clangcodeparser.cpp b/src/qdoc/clangcodeparser.cpp
index db9c2ed07..b5c6a8ac6 100644
--- a/src/qdoc/clangcodeparser.cpp
+++ b/src/qdoc/clangcodeparser.cpp
@@ -65,6 +65,9 @@ QT_BEGIN_NAMESPACE
static CXTranslationUnit_Flags flags_ = static_cast<CXTranslationUnit_Flags>(0);
static CXIndex index_ = nullptr;
+QByteArray ClangCodeParser::fn_;
+constexpr const char *fnDummyFileName = "/fn_dummyfile.cpp";
+
#ifndef QT_NO_DEBUG_STREAM
template<class T>
static QDebug operator<<(QDebug debug, const std::vector<T> &v)
@@ -199,11 +202,15 @@ static QString getSpelling(CXSourceRange range)
unsigned int offset1, offset2;
clang_getFileLocation(start, &file1, nullptr, nullptr, &offset1);
clang_getFileLocation(end, &file2, nullptr, nullptr, &offset2);
+
if (file1 != file2 || offset2 <= offset1)
return QString();
QFile file(fromCXString(clang_getFileName(file1)));
- if (!file.open(QFile::ReadOnly))
+ if (!file.open(QFile::ReadOnly)) {
+ if (file.fileName() == fnDummyFileName)
+ return QString::fromUtf8(ClangCodeParser::fn().mid(offset1, offset2 - offset1));
return QString();
+ }
file.seek(offset1);
return QString::fromUtf8(file.read(offset2 - offset1));
}
@@ -1595,6 +1602,7 @@ void ClangCodeParser::parseSourceFile(const Location & /*location*/, const QStri
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index_);
namespaceScope_.clear();
+ fn_.clear();
}
/*!
@@ -1673,17 +1681,17 @@ Node *ClangCodeParser::parseFnArg(const Location &location, const QString &fnArg
args.push_back(pchName_.constData());
}
CXTranslationUnit tu;
- QByteArray fn;
+ fn_.clear();
for (const auto &ns : qAsConst(namespaceScope_))
- fn.prepend("namespace " + ns.toUtf8() + " {");
- fn += fnArg.toUtf8();
- if (!fn.endsWith(";"))
- fn += "{ }";
- fn.append(namespaceScope_.size(), '}');
-
- const char *dummyFileName = "/fn_dummyfile.cpp";
- CXUnsavedFile unsavedFile { dummyFileName, fn.constData(),
- static_cast<unsigned long>(fn.size()) };
+ fn_.prepend("namespace " + ns.toUtf8() + " {");
+ fn_ += fnArg.toUtf8();
+ if (!fn_.endsWith(";"))
+ fn_ += "{ }";
+ fn_.append(namespaceScope_.size(), '}');
+
+ const char *dummyFileName = fnDummyFileName;
+ CXUnsavedFile unsavedFile { dummyFileName, fn_.constData(),
+ static_cast<unsigned long>(fn_.size()) };
CXErrorCode err = clang_parseTranslationUnit2(index, dummyFileName, args.data(), args.size(),
&unsavedFile, 1, flags, &tu);
qCDebug(lcQdoc) << __FUNCTION__ << "clang_parseTranslationUnit2(" << dummyFileName << args