2013-12-10 14:37:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangutils.h"
|
|
|
|
|
|
2015-10-13 15:56:41 +02:00
|
|
|
#include "clangeditordocumentprocessor.h"
|
2016-11-21 13:30:26 +01:00
|
|
|
#include "clangmodelmanagersupport.h"
|
2021-02-23 13:51:41 +01:00
|
|
|
#include "clangprojectsettings.h"
|
2015-10-13 15:56:41 +02:00
|
|
|
|
2013-12-10 14:37:32 +01:00
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/idocument.h>
|
2021-08-30 10:58:08 +02:00
|
|
|
#include <cppeditor/baseeditordocumentparser.h>
|
|
|
|
|
#include <cppeditor/compileroptionsbuilder.h>
|
|
|
|
|
#include <cppeditor/cppcodemodelsettings.h>
|
|
|
|
|
#include <cppeditor/cppmodelmanager.h>
|
|
|
|
|
#include <cppeditor/cpptoolsreuse.h>
|
|
|
|
|
#include <cppeditor/editordocumenthandle.h>
|
|
|
|
|
#include <cppeditor/projectpart.h>
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <projectexplorer/buildconfiguration.h>
|
2019-03-13 10:22:16 +01:00
|
|
|
#include <projectexplorer/kitinformation.h>
|
2015-11-17 16:25:02 +01:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <projectexplorer/target.h>
|
2021-06-18 16:30:03 +02:00
|
|
|
#include <texteditor/codeassist/textdocumentmanipulatorinterface.h>
|
2018-02-09 13:51:39 +01:00
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
#include <cplusplus/SimpleLexer.h>
|
2018-02-09 13:51:39 +01:00
|
|
|
#include <utils/algorithm.h>
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <utils/fileutils.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2014-01-21 14:57:00 +01:00
|
|
|
#include <QDir>
|
2013-12-10 14:37:32 +01:00
|
|
|
#include <QFile>
|
2018-07-10 15:53:51 +02:00
|
|
|
#include <QJsonArray>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QJsonObject>
|
2015-11-27 18:21:46 +01:00
|
|
|
#include <QStringList>
|
2018-02-01 13:27:50 +01:00
|
|
|
#include <QTextBlock>
|
2013-12-10 14:37:32 +01:00
|
|
|
|
|
|
|
|
using namespace Core;
|
2021-08-30 10:58:08 +02:00
|
|
|
using namespace CppEditor;
|
2020-02-18 18:25:26 +01:00
|
|
|
using namespace ProjectExplorer;
|
2020-07-06 15:49:35 +02:00
|
|
|
using namespace Utils;
|
2013-12-10 14:37:32 +01:00
|
|
|
|
|
|
|
|
namespace ClangCodeModel {
|
2020-07-06 15:49:35 +02:00
|
|
|
namespace Internal {
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2021-08-20 11:21:06 +02:00
|
|
|
ProjectPart::ConstPtr projectPartForFile(const QString &filePath)
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
2021-08-30 10:58:08 +02:00
|
|
|
if (const auto parser = CppEditor::BaseEditorDocumentParser::get(filePath))
|
2017-01-17 15:27:31 +01:00
|
|
|
return parser->projectPartInfo().projectPart;
|
2021-08-20 11:21:06 +02:00
|
|
|
return ProjectPart::ConstPtr();
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2018-05-09 15:12:01 +02:00
|
|
|
QString diagnosticCategoryPrefixRemoved(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
QString theText = text;
|
|
|
|
|
|
|
|
|
|
// Prefixes are taken from $LLVM_SOURCE_DIR/tools/clang/lib/Frontend/TextDiagnostic.cpp,
|
|
|
|
|
// function TextDiagnostic::printDiagnosticLevel (llvm-3.6.2).
|
|
|
|
|
static const QStringList categoryPrefixes = {
|
|
|
|
|
QStringLiteral("note"),
|
|
|
|
|
QStringLiteral("remark"),
|
|
|
|
|
QStringLiteral("warning"),
|
|
|
|
|
QStringLiteral("error"),
|
|
|
|
|
QStringLiteral("fatal error")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const QString &prefix : categoryPrefixes) {
|
|
|
|
|
const QString fullPrefix = prefix + QStringLiteral(": ");
|
|
|
|
|
if (theText.startsWith(fullPrefix)) {
|
|
|
|
|
theText.remove(0, fullPrefix.length());
|
|
|
|
|
return theText;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-13 10:22:16 +01:00
|
|
|
static QStringList projectPartArguments(const ProjectPart &projectPart)
|
2018-07-10 15:53:51 +02:00
|
|
|
{
|
2019-03-13 10:22:16 +01:00
|
|
|
QStringList args;
|
2021-05-07 16:10:07 +02:00
|
|
|
args << projectPart.compilerFilePath.toString();
|
2019-03-13 10:22:16 +01:00
|
|
|
args << "-c";
|
|
|
|
|
if (projectPart.toolchainType != ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
|
|
|
|
|
args << "--target=" + projectPart.toolChainTargetTriple;
|
|
|
|
|
args << (projectPart.toolChainWordWidth == ProjectPart::WordWidth64Bit
|
|
|
|
|
? QLatin1String("-m64")
|
|
|
|
|
: QLatin1String("-m32"));
|
|
|
|
|
}
|
|
|
|
|
args << projectPart.compilerFlags;
|
|
|
|
|
for (const ProjectExplorer::HeaderPath &headerPath : projectPart.headerPaths) {
|
|
|
|
|
if (headerPath.type == ProjectExplorer::HeaderPathType::User) {
|
|
|
|
|
args << "-I" + headerPath.path;
|
|
|
|
|
} else if (headerPath.type == ProjectExplorer::HeaderPathType::System) {
|
|
|
|
|
args << (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID
|
|
|
|
|
? "-I"
|
|
|
|
|
: "-isystem")
|
|
|
|
|
+ headerPath.path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (const ProjectExplorer::Macro ¯o : projectPart.projectMacros) {
|
|
|
|
|
args.append(QString::fromUtf8(
|
|
|
|
|
macro.toKeyValue(macro.type == ProjectExplorer::MacroType::Define ? "-D" : "-U")));
|
|
|
|
|
}
|
2018-07-10 15:53:51 +02:00
|
|
|
|
2019-03-13 10:22:16 +01:00
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 15:49:35 +02:00
|
|
|
static QJsonObject createFileObject(const FilePath &buildDir,
|
2019-03-13 10:22:16 +01:00
|
|
|
const QStringList &arguments,
|
|
|
|
|
const ProjectPart &projectPart,
|
2021-02-23 13:51:41 +01:00
|
|
|
const ProjectFile &projFile,
|
2021-05-07 16:10:07 +02:00
|
|
|
CompilationDbPurpose purpose,
|
2022-05-05 17:34:31 +02:00
|
|
|
const QJsonArray &projectPartOptions,
|
2022-05-05 15:50:46 +02:00
|
|
|
UsePrecompiledHeaders usePch)
|
2019-03-13 10:22:16 +01:00
|
|
|
{
|
2018-07-10 15:53:51 +02:00
|
|
|
QJsonObject fileObject;
|
|
|
|
|
fileObject["file"] = projFile.path;
|
2021-02-23 13:51:41 +01:00
|
|
|
QJsonArray args;
|
|
|
|
|
|
|
|
|
|
if (purpose == CompilationDbPurpose::Project) {
|
|
|
|
|
args = QJsonArray::fromStringList(arguments);
|
|
|
|
|
|
|
|
|
|
const ProjectFile::Kind kind = ProjectFile::classify(projFile.path);
|
|
|
|
|
if (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID
|
|
|
|
|
|| projectPart.toolchainType == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) {
|
|
|
|
|
if (ProjectFile::isC(kind))
|
|
|
|
|
args.append("/TC");
|
|
|
|
|
else if (ProjectFile::isCxx(kind))
|
|
|
|
|
args.append("/TP");
|
|
|
|
|
} else {
|
|
|
|
|
QStringList langOption
|
|
|
|
|
= createLanguageOptionGcc(kind,
|
|
|
|
|
projectPart.languageExtensions
|
|
|
|
|
& LanguageExtension::ObjectiveC);
|
|
|
|
|
for (const QString &langOptionPart : langOption)
|
|
|
|
|
args.append(langOptionPart);
|
|
|
|
|
}
|
2019-03-13 10:22:16 +01:00
|
|
|
} else {
|
2022-05-05 17:34:31 +02:00
|
|
|
args = clangOptionsForFile(projFile, projectPart, projectPartOptions, usePch);
|
2021-02-23 13:51:41 +01:00
|
|
|
args.prepend("clang"); // TODO: clang-cl for MSVC targets? Does it matter at all what we put here?
|
2019-03-13 10:22:16 +01:00
|
|
|
}
|
2021-02-23 13:51:41 +01:00
|
|
|
|
2018-07-10 15:53:51 +02:00
|
|
|
args.append(QDir::toNativeSeparators(projFile.path));
|
|
|
|
|
fileObject["arguments"] = args;
|
|
|
|
|
fileObject["directory"] = buildDir.toString();
|
|
|
|
|
return fileObject;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 10:58:08 +02:00
|
|
|
GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo,
|
2021-10-15 16:24:25 +02:00
|
|
|
const Utils::FilePath &baseDir,
|
2021-05-07 16:10:07 +02:00
|
|
|
CompilationDbPurpose purpose,
|
|
|
|
|
const ClangDiagnosticConfig &warningsConfig,
|
|
|
|
|
const QStringList &projectOptions)
|
2018-07-10 15:53:51 +02:00
|
|
|
{
|
2021-10-15 16:24:25 +02:00
|
|
|
QTC_ASSERT(!baseDir.isEmpty(), return GenerateCompilationDbResult(QString(),
|
2019-03-13 17:58:06 +01:00
|
|
|
QCoreApplication::translate("ClangUtils", "Could not retrieve build directory.")));
|
2022-03-11 12:40:14 +01:00
|
|
|
QTC_ASSERT(projectInfo, return GenerateCompilationDbResult(QString(),
|
|
|
|
|
"Could not retrieve project info."));
|
2021-10-15 16:24:25 +02:00
|
|
|
QTC_CHECK(baseDir.ensureWritableDir());
|
|
|
|
|
QFile compileCommandsFile(baseDir.toString() + "/compile_commands.json");
|
2018-12-03 08:26:48 +01:00
|
|
|
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
2019-03-13 17:58:06 +01:00
|
|
|
if (!fileOpened) {
|
|
|
|
|
return GenerateCompilationDbResult(QString(),
|
|
|
|
|
QCoreApplication::translate("ClangUtils", "Could not create \"%1\": %2")
|
|
|
|
|
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString()));
|
|
|
|
|
}
|
2018-09-20 00:42:28 +03:00
|
|
|
compileCommandsFile.write("[");
|
2018-07-10 15:53:51 +02:00
|
|
|
|
2022-05-05 15:50:46 +02:00
|
|
|
const UsePrecompiledHeaders usePch = getPchUsage();
|
2022-05-05 17:34:31 +02:00
|
|
|
const QJsonArray jsonProjectOptions = QJsonArray::fromStringList(projectOptions);
|
2021-08-20 11:21:06 +02:00
|
|
|
for (ProjectPart::ConstPtr projectPart : projectInfo->projectParts()) {
|
2021-02-23 13:51:41 +01:00
|
|
|
QStringList args;
|
2022-05-04 16:48:48 +02:00
|
|
|
const CompilerOptionsBuilder optionsBuilder = clangOptionsBuilder(*projectPart,
|
|
|
|
|
warningsConfig);
|
2022-05-05 17:34:31 +02:00
|
|
|
QJsonArray ppOptions;
|
|
|
|
|
if (purpose == CompilationDbPurpose::Project) {
|
2021-02-23 13:51:41 +01:00
|
|
|
args = projectPartArguments(*projectPart);
|
2022-05-05 17:34:31 +02:00
|
|
|
} else {
|
|
|
|
|
ppOptions = fullProjectPartOptions(projectPartOptions(optionsBuilder), jsonProjectOptions);
|
|
|
|
|
}
|
2018-09-20 00:42:28 +03:00
|
|
|
for (const ProjectFile &projFile : projectPart->files) {
|
2022-05-05 17:34:31 +02:00
|
|
|
const QJsonObject json = createFileObject(baseDir, args, *projectPart, projFile,
|
|
|
|
|
purpose, ppOptions, usePch);
|
2018-09-20 00:42:28 +03:00
|
|
|
if (compileCommandsFile.size() > 1)
|
|
|
|
|
compileCommandsFile.write(",");
|
|
|
|
|
compileCommandsFile.write('\n' + QJsonDocument(json).toJson().trimmed());
|
|
|
|
|
}
|
2018-07-10 15:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 00:42:28 +03:00
|
|
|
compileCommandsFile.write("\n]");
|
2018-07-10 15:53:51 +02:00
|
|
|
compileCommandsFile.close();
|
2019-03-13 17:58:06 +01:00
|
|
|
return GenerateCompilationDbResult(compileCommandsFile.fileName(), QString());
|
2018-07-10 15:53:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 16:03:38 +01:00
|
|
|
QString currentCppEditorDocumentFilePath()
|
|
|
|
|
{
|
|
|
|
|
QString filePath;
|
|
|
|
|
|
|
|
|
|
const auto currentEditor = Core::EditorManager::currentEditor();
|
2021-08-30 10:58:08 +02:00
|
|
|
if (currentEditor && CppEditor::CppModelManager::isCppEditor(currentEditor)) {
|
2019-01-29 16:03:38 +01:00
|
|
|
const auto currentDocument = currentEditor->document();
|
|
|
|
|
if (currentDocument)
|
|
|
|
|
filePath = currentDocument->filePath().toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filePath;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-28 12:40:03 +01:00
|
|
|
DiagnosticTextInfo::DiagnosticTextInfo(const QString &text)
|
|
|
|
|
: m_text(text)
|
|
|
|
|
, m_squareBracketStartIndex(text.lastIndexOf('['))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
QString DiagnosticTextInfo::textWithoutOption() const
|
|
|
|
|
{
|
|
|
|
|
if (m_squareBracketStartIndex == -1)
|
|
|
|
|
return m_text;
|
|
|
|
|
|
|
|
|
|
return m_text.mid(0, m_squareBracketStartIndex - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiagnosticTextInfo::option() const
|
|
|
|
|
{
|
|
|
|
|
if (m_squareBracketStartIndex == -1)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
const int index = m_squareBracketStartIndex + 1;
|
|
|
|
|
return m_text.mid(index, m_text.count() - index - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiagnosticTextInfo::category() const
|
|
|
|
|
{
|
|
|
|
|
if (m_squareBracketStartIndex == -1)
|
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
const int index = m_squareBracketStartIndex + 1;
|
|
|
|
|
if (isClazyOption(m_text.mid(index)))
|
|
|
|
|
return QCoreApplication::translate("ClangDiagnosticWidget", "Clazy Issue");
|
|
|
|
|
else
|
|
|
|
|
return QCoreApplication::translate("ClangDiagnosticWidget", "Clang-Tidy Issue");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DiagnosticTextInfo::isClazyOption(const QString &option)
|
|
|
|
|
{
|
|
|
|
|
return option.startsWith("-Wclazy");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString DiagnosticTextInfo::clazyCheckName(const QString &option)
|
|
|
|
|
{
|
|
|
|
|
if (option.startsWith("-Wclazy"))
|
|
|
|
|
return option.mid(8); // Chop "-Wclazy-"
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 13:51:41 +01:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
static ClangProjectSettings &getProjectSettings(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(project);
|
|
|
|
|
return ClangModelManagerSupport::instance()->projectSettings(project);
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2022-05-05 17:34:31 +02:00
|
|
|
QJsonArray clangOptionsForFile(const ProjectFile &file, const ProjectPart &projectPart,
|
|
|
|
|
const QJsonArray &generalOptions, UsePrecompiledHeaders usePch)
|
2021-02-23 13:51:41 +01:00
|
|
|
{
|
2022-05-05 17:34:31 +02:00
|
|
|
CompilerOptionsBuilder optionsBuilder(projectPart);
|
2022-05-05 15:36:58 +02:00
|
|
|
ProjectFile::Kind fileKind = file.kind;
|
2022-05-04 16:48:48 +02:00
|
|
|
if (fileKind == ProjectFile::AmbiguousHeader) {
|
2022-05-05 17:34:31 +02:00
|
|
|
fileKind = projectPart.languageVersion <= LanguageVersion::LatestC
|
2022-05-04 16:48:48 +02:00
|
|
|
? ProjectFile::CHeader : ProjectFile::CXXHeader;
|
|
|
|
|
}
|
2022-05-05 15:50:46 +02:00
|
|
|
if (usePch == UsePrecompiledHeaders::Yes
|
2022-05-05 17:34:31 +02:00
|
|
|
&& projectPart.precompiledHeaders.contains(file.path)) {
|
2022-05-05 15:50:46 +02:00
|
|
|
usePch = UsePrecompiledHeaders::No;
|
2022-05-04 16:48:48 +02:00
|
|
|
}
|
2022-05-05 15:36:58 +02:00
|
|
|
optionsBuilder.updateFileLanguage(file.kind);
|
2022-05-05 15:50:46 +02:00
|
|
|
optionsBuilder.addPrecompiledHeaderOptions(usePch);
|
2022-05-05 17:34:31 +02:00
|
|
|
const QJsonArray specificOptions = QJsonArray::fromStringList(optionsBuilder.options());
|
|
|
|
|
QJsonArray fullOptions = generalOptions;
|
|
|
|
|
for (const QJsonValue &opt : specificOptions)
|
|
|
|
|
fullOptions << opt;
|
|
|
|
|
return fullOptions;
|
2021-05-07 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClangDiagnosticConfig warningsConfigForProject(Project *project)
|
|
|
|
|
{
|
|
|
|
|
if (project) {
|
|
|
|
|
ClangProjectSettings &projectSettings = ClangModelManagerSupport::instance()
|
|
|
|
|
->projectSettings(project);
|
|
|
|
|
if (!projectSettings.useGlobalConfig()) {
|
|
|
|
|
const Utils::Id warningConfigId = projectSettings.warningConfigId();
|
2021-08-30 10:58:08 +02:00
|
|
|
const CppEditor::ClangDiagnosticConfigsModel configsModel
|
|
|
|
|
= CppEditor::diagnosticConfigsModel();
|
2021-05-07 16:10:07 +02:00
|
|
|
if (configsModel.hasConfigWithId(warningConfigId))
|
|
|
|
|
return configsModel.configWithId(warningConfigId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-30 10:58:08 +02:00
|
|
|
return CppEditor::codeModelSettings()->clangDiagnosticConfig();
|
2021-05-07 16:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-04 16:48:48 +02:00
|
|
|
const QStringList optionsForProject(ProjectExplorer::Project *project,
|
|
|
|
|
const ClangDiagnosticConfig &warningsConfig)
|
2021-05-07 16:10:07 +02:00
|
|
|
{
|
2022-05-04 16:48:48 +02:00
|
|
|
QStringList options = ClangDiagnosticConfigsModel::globalDiagnosticOptions();
|
|
|
|
|
if (!warningsConfig.useBuildSystemWarnings()) {
|
|
|
|
|
options += project
|
|
|
|
|
? getProjectSettings(project).commandLineOptions()
|
|
|
|
|
: ClangProjectSettings::globalCommandLineOptions();
|
|
|
|
|
}
|
|
|
|
|
return options;
|
2021-02-23 13:51:41 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-18 16:30:03 +02:00
|
|
|
// 7.3.3: using typename(opt) nested-name-specifier unqualified-id ;
|
|
|
|
|
bool isAtUsingDeclaration(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
|
|
|
|
int basePosition)
|
|
|
|
|
{
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
SimpleLexer lexer;
|
|
|
|
|
lexer.setLanguageFeatures(LanguageFeatures::defaultFeatures());
|
|
|
|
|
const QString textToLex = textUntilPreviousStatement(manipulator, basePosition);
|
|
|
|
|
const Tokens tokens = lexer(textToLex);
|
|
|
|
|
if (tokens.empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// The nested-name-specifier always ends with "::", so check for this first.
|
|
|
|
|
const Token lastToken = tokens[tokens.size() - 1];
|
|
|
|
|
if (lastToken.kind() != T_COLON_COLON)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return contains(tokens, [](const Token &token) { return token.kind() == T_USING; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString textUntilPreviousStatement(TextEditor::TextDocumentManipulatorInterface &manipulator,
|
|
|
|
|
int startPosition)
|
|
|
|
|
{
|
|
|
|
|
static const QString stopCharacters(";{}#");
|
|
|
|
|
|
|
|
|
|
int endPosition = 0;
|
|
|
|
|
for (int i = startPosition; i >= 0 ; --i) {
|
|
|
|
|
if (stopCharacters.contains(manipulator.characterAt(i))) {
|
|
|
|
|
endPosition = i + 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return manipulator.textAt(endPosition, startPosition - endPosition);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 16:48:48 +02:00
|
|
|
CompilerOptionsBuilder clangOptionsBuilder(const ProjectPart &projectPart,
|
|
|
|
|
const ClangDiagnosticConfig &warningsConfig)
|
|
|
|
|
{
|
|
|
|
|
const auto useBuildSystemWarnings = warningsConfig.useBuildSystemWarnings()
|
|
|
|
|
? UseBuildSystemWarnings::Yes
|
|
|
|
|
: UseBuildSystemWarnings::No;
|
2022-05-09 14:59:04 +02:00
|
|
|
const FilePath clangIncludeDir = Core::ICore::clangIncludeDirectory(
|
|
|
|
|
QString(CLANG_VERSION), FilePath(CLANG_INCLUDE_DIR));
|
2022-05-04 16:48:48 +02:00
|
|
|
CompilerOptionsBuilder optionsBuilder(projectPart, UseSystemHeader::No,
|
|
|
|
|
UseTweakedHeaderPaths::Yes, UseLanguageDefines::No,
|
2022-05-09 14:59:04 +02:00
|
|
|
useBuildSystemWarnings, clangIncludeDir);
|
2022-05-04 16:48:48 +02:00
|
|
|
optionsBuilder.provideAdditionalMacros({ProjectExplorer::Macro("Q_CREATOR_RUN", "1")});
|
|
|
|
|
optionsBuilder.build(ProjectFile::Unclassified, UsePrecompiledHeaders::No);
|
|
|
|
|
const QString uiIncludePath
|
|
|
|
|
= ClangModelManagerSupport::instance()->dummyUiHeaderOnDiskDirPath();
|
|
|
|
|
if (!uiIncludePath.isEmpty()) {
|
|
|
|
|
optionsBuilder.prepend(QDir::toNativeSeparators(uiIncludePath));
|
|
|
|
|
optionsBuilder.prepend("-I");
|
|
|
|
|
}
|
|
|
|
|
optionsBuilder.add("-fmessage-length=0", /*gccOnlyOption=*/true);
|
|
|
|
|
optionsBuilder.add("-fdiagnostics-show-note-include-stack", /*gccOnlyOption=*/true);
|
|
|
|
|
optionsBuilder.add("-fretain-comments-from-system-headers", /*gccOnlyOption=*/true);
|
|
|
|
|
optionsBuilder.add("-fmacro-backtrace-limit=0");
|
|
|
|
|
optionsBuilder.add("-ferror-limit=1000");
|
|
|
|
|
|
|
|
|
|
if (useBuildSystemWarnings == UseBuildSystemWarnings::No) {
|
|
|
|
|
for (const QString &opt : warningsConfig.clangOptions())
|
|
|
|
|
optionsBuilder.add(opt, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return optionsBuilder;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 17:34:31 +02:00
|
|
|
QJsonArray projectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder)
|
|
|
|
|
{
|
|
|
|
|
const QStringList optionsList = optionsBuilder.options();
|
|
|
|
|
QJsonArray optionsArray;
|
|
|
|
|
for (const QString &opt : optionsList) {
|
|
|
|
|
// These will be added later by the file-specific code, and they trigger warnings
|
|
|
|
|
// if they appear twice; see QTCREATORBUG-26664.
|
|
|
|
|
if (opt != "-TP" && opt != "-TC")
|
|
|
|
|
optionsArray << opt;
|
|
|
|
|
}
|
|
|
|
|
return optionsArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray fullProjectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder,
|
|
|
|
|
const QStringList &projectOptions)
|
|
|
|
|
{
|
|
|
|
|
return fullProjectPartOptions(projectPartOptions(optionsBuilder),
|
|
|
|
|
QJsonArray::fromStringList(projectOptions));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonArray fullProjectPartOptions(const QJsonArray &projectPartOptions,
|
|
|
|
|
const QJsonArray &projectOptions)
|
|
|
|
|
{
|
|
|
|
|
QJsonArray fullProjectPartOptions = projectPartOptions;
|
|
|
|
|
for (const QJsonValue &opt : projectOptions)
|
|
|
|
|
fullProjectPartOptions.prepend(opt);
|
|
|
|
|
return fullProjectPartOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 15:49:35 +02:00
|
|
|
} // namespace Internal
|
2013-12-10 14:37:32 +01:00
|
|
|
} // namespace Clang
|