Commit ca1d1dfb authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Add possibility to "pgo-train" libclang with a batch file

This allows to start Qt Creator in batch processing mode:

  $ export QTC_CLANG_BATCH=/path/to/file
  $ export QT_LOGGING_RULES=qtc.clangcodemodel.batch=true
  $ ./qtcreator -load ClangCodeModel

The batch file will be executed and Qt Creator will exit. Advanced
logging output can be activated as stated above.

Note that it is required that the project was already configured/set up
properly with the used settingspath, otherwise the wrong configuration
will be taken or a pop-up dialog will block the execution.

A small example follows that covers all the understood and so far needed
batch file commands in order to train libclang for profile guided
optimization. ${PWD} expands to the directory of the batch file.

    openProject "${PWD}/calendarwidget.pro"

    # Initial parsing
    openDocument "${PWD}/window.cpp"
    closeAllDocuments
    openDocument "${PWD}/window.cpp"

    # Reparse
    setCursor 478 1
    insertText " "
    insertText " "
    insertText " "

    # Completion
    complete
    complete
    complete

    # Member completion
    insertText "comboBox->"
    complete
    complete
    complete

    # Wait in order to inspect the result
    processEvents 3000

Change-Id: I7dc5dddc6752272ecc2fb4f30497b17cee3f9a9f
Reviewed-by: default avatarhjk <hjk@qt.io>
Reviewed-by: David Schulz's avatarDavid Schulz <david.schulz@qt.io>
parent f8ad72de
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#include "clangautomationutils.h"
#include "clangcompletionassistinterface.h"
#include "clangcompletionassistprovider.h"
#include <texteditor/codeassist/assistinterface.h>
#include <texteditor/codeassist/assistproposalitem.h>
#include <texteditor/codeassist/completionassistprovider.h>
#include <texteditor/codeassist/genericproposalmodel.h>
#include <texteditor/codeassist/iassistprocessor.h>
#include <texteditor/codeassist/iassistproposal.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <utils/qtcassert.h>
#include <QCoreApplication>
#include <QElapsedTimer>
namespace ClangCodeModel {
namespace Internal {
class WaitForAsyncCompletions
{
public:
enum WaitResult { GotResults, GotInvalidResults, Timeout };
WaitResult wait(TextEditor::IAssistProcessor *processor,
TextEditor::AssistInterface *assistInterface,
int timeoutInMs)
{
QTC_ASSERT(processor, return Timeout);
QTC_ASSERT(assistInterface, return Timeout);
bool gotResults = false;
processor->setAsyncCompletionAvailableHandler(
[this, &gotResults] (TextEditor::IAssistProposal *proposal) {
QTC_ASSERT(proposal, return);
proposalModel = proposal->model();
delete proposal;
gotResults = true;
});
// Are there any immediate results?
if (TextEditor::IAssistProposal *proposal = processor->perform(assistInterface)) {
delete processor;
proposalModel = proposal->model();
delete proposal;
QTC_ASSERT(proposalModel, return GotInvalidResults);
return GotResults;
}
// There are not any, so wait for async results.
QElapsedTimer timer;
timer.start();
while (!gotResults) {
if (timer.elapsed() >= timeoutInMs)
return Timeout;
QCoreApplication::processEvents();
}
return proposalModel ? GotResults : GotInvalidResults;
}
public:
TextEditor::IAssistProposalModel *proposalModel;
};
static const CppTools::ProjectPartHeaderPaths toHeaderPaths(const QStringList &paths)
{
using namespace CppTools;
ProjectPartHeaderPaths result;
foreach (const QString &path, paths)
result << ProjectPartHeaderPath(path, ProjectPartHeaderPath::IncludePath);
return result;
}
ProposalModel completionResults(TextEditor::BaseTextEditor *textEditor,
const QStringList &includePaths,
int timeOutInMs)
{
using namespace TextEditor;
TextEditorWidget *textEditorWidget = qobject_cast<TextEditorWidget *>(textEditor->widget());
QTC_ASSERT(textEditorWidget, return ProposalModel());
AssistInterface *assistInterface = textEditorWidget->createAssistInterface(
TextEditor::Completion, TextEditor::ExplicitlyInvoked);
QTC_ASSERT(assistInterface, return ProposalModel());
if (!includePaths.isEmpty()) {
auto clangAssistInterface = static_cast<ClangCompletionAssistInterface *>(assistInterface);
clangAssistInterface->setHeaderPaths(toHeaderPaths(includePaths));
}
CompletionAssistProvider *assistProvider
= textEditor->textDocument()->completionAssistProvider();
QTC_ASSERT(qobject_cast<ClangCompletionAssistProvider *>(assistProvider),
return ProposalModel());
QTC_ASSERT(assistProvider, return ProposalModel());
QTC_ASSERT(assistProvider->runType() == IAssistProvider::Asynchronous, return ProposalModel());
IAssistProcessor *processor = assistProvider->createProcessor();
QTC_ASSERT(processor, return ProposalModel());
WaitForAsyncCompletions waitForCompletions;
const WaitForAsyncCompletions::WaitResult result = waitForCompletions.wait(processor,
assistInterface,
timeOutInMs);
QTC_ASSERT(result == WaitForAsyncCompletions::GotResults, return ProposalModel());
return QSharedPointer<TextEditor::IAssistProposalModel>(waitForCompletions.proposalModel);
}
} // namespace Internal
} // namespace ClangCodeModel
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#pragma once
#include <QString>
#include <QSharedPointer>
namespace TextEditor {
class BaseTextEditor;
class IAssistProposalModel;
}
namespace ClangCodeModel {
namespace Internal {
using ProposalModel = QSharedPointer<TextEditor::IAssistProposalModel>;
ProposalModel completionResults(TextEditor::BaseTextEditor *textEditor,
const QStringList &includePaths = QStringList(),
int timeOutInMs = 10000);
} // namespace Internal
} // namespace ClangCodeModel
This diff is collapsed.
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** 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.
**
** 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.
**
****************************************************************************/
#pragma once
#include <QString>
namespace ClangCodeModel {
namespace Internal {
bool runClangBatchFile(const QString &filePath);
} // namespace Internal
} // namespace ClangCodeModel
...@@ -9,7 +9,9 @@ SOURCES += \ ...@@ -9,7 +9,9 @@ SOURCES += \
clangassistproposal.cpp \ clangassistproposal.cpp \
clangassistproposalitem.cpp \ clangassistproposalitem.cpp \
clangassistproposalmodel.cpp \ clangassistproposalmodel.cpp \
clangautomationutils.cpp \
clangbackendipcintegration.cpp \ clangbackendipcintegration.cpp \
clangbatchfileprocessor.cpp \
clangcodemodelplugin.cpp \ clangcodemodelplugin.cpp \
clangcompletionassistinterface.cpp \ clangcompletionassistinterface.cpp \
clangcompletionassistprocessor.cpp \ clangcompletionassistprocessor.cpp \
...@@ -39,7 +41,9 @@ HEADERS += \ ...@@ -39,7 +41,9 @@ HEADERS += \
clangassistproposal.h \ clangassistproposal.h \
clangassistproposalitem.h \ clangassistproposalitem.h \
clangassistproposalmodel.h \ clangassistproposalmodel.h \
clangautomationutils.h \
clangbackendipcintegration.h \ clangbackendipcintegration.h \
clangbatchfileprocessor.h \
clangcodemodelplugin.h \ clangcodemodelplugin.h \
clangcompletionassistinterface.h \ clangcompletionassistinterface.h \
clangcompletionassistprocessor.h \ clangcompletionassistprocessor.h \
......
...@@ -41,8 +41,12 @@ QtcPlugin { ...@@ -41,8 +41,12 @@ QtcPlugin {
"clangassistproposalitem.h", "clangassistproposalitem.h",
"clangassistproposalmodel.cpp", "clangassistproposalmodel.cpp",
"clangassistproposalmodel.h", "clangassistproposalmodel.h",
"clangautomationutils.cpp",
"clangautomationutils.h",
"clangbackendipcintegration.cpp", "clangbackendipcintegration.cpp",
"clangbackendipcintegration.h", "clangbackendipcintegration.h",
"clangbatchfileprocessor.cpp",
"clangbatchfileprocessor.h",
"clangcodemodel.qrc", "clangcodemodel.qrc",
"clangcodemodelplugin.cpp", "clangcodemodelplugin.cpp",
"clangcodemodelplugin.h", "clangcodemodelplugin.h",
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "clangcodemodelplugin.h" #include "clangcodemodelplugin.h"
#include "clangbatchfileprocessor.h"
#include "clangconstants.h" #include "clangconstants.h"
#include "clangprojectsettingswidget.h" #include "clangprojectsettingswidget.h"
...@@ -74,8 +75,13 @@ void addProjectPanelWidget() ...@@ -74,8 +75,13 @@ void addProjectPanelWidget()
bool ClangCodeModelPlugin::initialize(const QStringList &arguments, QString *errorMessage) bool ClangCodeModelPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{ {
Q_UNUSED(arguments) Q_UNUSED(arguments);
Q_UNUSED(errorMessage) Q_UNUSED(errorMessage);
connect(ProjectExplorer::ProjectExplorerPlugin::instance(),
&ProjectExplorer::ProjectExplorerPlugin::finishedInitialization,
this,
&ClangCodeModelPlugin::maybeHandleBatchFileAndExit);
CppTools::CppModelManager::instance()->activateClangCodeModel(&m_modelManagerSupportProvider); CppTools::CppModelManager::instance()->activateClangCodeModel(&m_modelManagerSupportProvider);
...@@ -89,6 +95,16 @@ void ClangCodeModelPlugin::extensionsInitialized() ...@@ -89,6 +95,16 @@ void ClangCodeModelPlugin::extensionsInitialized()
{ {
} }
// For e.g. creation of profile-guided optimization builds.
void ClangCodeModelPlugin::maybeHandleBatchFileAndExit() const
{
const QString batchFilePath = QString::fromLocal8Bit(qgetenv("QTC_CLANG_BATCH"));
if (!batchFilePath.isEmpty() && QTC_GUARD(QFileInfo::exists(batchFilePath))) {
const bool runSucceeded = runClangBatchFile(batchFilePath);
QCoreApplication::exit(!runSucceeded);
}
}
#ifdef WITH_TESTS #ifdef WITH_TESTS
QList<QObject *> ClangCodeModelPlugin::createTestObjects() const QList<QObject *> ClangCodeModelPlugin::createTestObjects() const
{ {
......
...@@ -41,6 +41,9 @@ public: ...@@ -41,6 +41,9 @@ public:
bool initialize(const QStringList &arguments, QString *errorMessage); bool initialize(const QStringList &arguments, QString *errorMessage);
void extensionsInitialized(); void extensionsInitialized();
private:
void maybeHandleBatchFileAndExit() const;
private: private:
ModelManagerSupportProviderClang m_modelManagerSupportProvider; ModelManagerSupportProviderClang m_modelManagerSupportProvider;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "clangcodecompletion_test.h" #include "clangcodecompletion_test.h"
#include "../clangautomationutils.h"
#include "../clangbackendipcintegration.h" #include "../clangbackendipcintegration.h"
#include "../clangcompletionassistinterface.h" #include "../clangcompletionassistinterface.h"
#include "../clangmodelmanagersupport.h" #include "../clangmodelmanagersupport.h"
...@@ -38,12 +39,8 @@ ...@@ -38,12 +39,8 @@
#include <cpptools/cpptoolstestcase.h> #include <cpptools/cpptoolstestcase.h>
#include <cpptools/modelmanagertesthelper.h> #include <cpptools/modelmanagertesthelper.h>
#include <cpptools/projectinfo.h> #include <cpptools/projectinfo.h>
#include <texteditor/codeassist/assistinterface.h>
#include <texteditor/codeassist/assistproposalitem.h> #include <texteditor/codeassist/assistproposalitem.h>
#include <texteditor/codeassist/completionassistprovider.h>
#include <texteditor/codeassist/genericproposalmodel.h> #include <texteditor/codeassist/genericproposalmodel.h>
#include <texteditor/codeassist/iassistprocessor.h>
#include <texteditor/codeassist/iassistproposal.h>
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditor.h> #include <texteditor/texteditor.h>
...@@ -180,53 +177,6 @@ void insertTextAtTopOfEditor(TextEditor::BaseTextEditor *editor, const QByteArra ...@@ -180,53 +177,6 @@ void insertTextAtTopOfEditor(TextEditor::BaseTextEditor *editor, const QByteArra
cs.apply(&textCursor); cs.apply(&textCursor);
} }
class WaitForAsyncCompletions
{
public:
enum WaitResult { GotResults, GotInvalidResults, Timeout };
WaitResult wait(TextEditor::IAssistProcessor *processor,
TextEditor::AssistInterface *assistInterface);
TextEditor::IAssistProposalModel *proposalModel;
};
WaitForAsyncCompletions::WaitResult WaitForAsyncCompletions::wait(
TextEditor::IAssistProcessor *processor,
TextEditor::AssistInterface *assistInterface)
{
QTC_ASSERT(processor, return Timeout);
QTC_ASSERT(assistInterface, return Timeout);
bool gotResults = false;
processor->setAsyncCompletionAvailableHandler(
[this, &gotResults] (TextEditor::IAssistProposal *proposal) {
QTC_ASSERT(proposal, return);
proposalModel = proposal->model();
delete proposal;
gotResults = true;
});
// Are there any immediate results?
if (TextEditor::IAssistProposal *proposal = processor->perform(assistInterface)) {
delete processor;
proposalModel = proposal->model();
delete proposal;
QTC_ASSERT(proposalModel, return GotInvalidResults);
return GotResults;
}
// There are not any, so wait for async results.
QElapsedTimer timer; timer.start();
while (!gotResults) {
if (timer.elapsed() >= 30 * 1000)
return Timeout;
QCoreApplication::processEvents();
}
return proposalModel ? GotResults : GotInvalidResults;
}
class ChangeDocumentReloadSetting class ChangeDocumentReloadSetting
{ {
public: public:
...@@ -421,51 +371,6 @@ public: ...@@ -421,51 +371,6 @@ public:
QString senderLog; QString senderLog;
}; };
const CppTools::ProjectPartHeaderPaths toHeaderPaths(const QStringList &paths)
{
using namespace CppTools;
ProjectPartHeaderPaths result;
foreach (const QString &path, paths)
result << ProjectPartHeaderPath(path, ProjectPartHeaderPath::IncludePath);
return result;
}
using ProposalModel = QSharedPointer<TextEditor::IAssistProposalModel>;
ProposalModel completionResults(
TextEditor::BaseTextEditor *textEditor,
const QStringList &includePaths = QStringList())
{
using namespace TextEditor;
TextEditorWidget *textEditorWidget = qobject_cast<TextEditorWidget *>(textEditor->widget());
QTC_ASSERT(textEditorWidget, return ProposalModel());
AssistInterface *assistInterface = textEditorWidget->createAssistInterface(
TextEditor::Completion, TextEditor::ExplicitlyInvoked);
QTC_ASSERT(assistInterface, return ProposalModel());
if (!includePaths.isEmpty()) {
auto clangAssistInterface = static_cast<ClangCompletionAssistInterface *>(assistInterface);
clangAssistInterface->setHeaderPaths(toHeaderPaths(includePaths));
}
CompletionAssistProvider *assistProvider
= textEditor->textDocument()->completionAssistProvider();
QTC_ASSERT(qobject_cast<ClangCompletionAssistProvider *>(assistProvider),
return ProposalModel());
QTC_ASSERT(assistProvider, return ProposalModel());
QTC_ASSERT(assistProvider->runType() == IAssistProvider::Asynchronous, return ProposalModel());
IAssistProcessor *processor = assistProvider->createProcessor();
QTC_ASSERT(processor, return ProposalModel());
WaitForAsyncCompletions waitForCompletions;
const WaitForAsyncCompletions::WaitResult result = waitForCompletions.wait(processor,
assistInterface);
QTC_ASSERT(result == WaitForAsyncCompletions::GotResults, return ProposalModel());
return QSharedPointer<TextEditor::IAssistProposalModel>(waitForCompletions.proposalModel);
}
class TestDocument class TestDocument
{ {
public: public:
...@@ -689,7 +594,7 @@ public: ...@@ -689,7 +594,7 @@ public:
if (!textToInsert.isEmpty()) if (!textToInsert.isEmpty())
openEditor.editor()->insert(textToInsert); openEditor.editor()->insert(textToInsert);
proposal = completionResults(openEditor.editor(), includePaths); proposal = completionResults(openEditor.editor(), includePaths, 15000);
} }
ProposalModel proposal; ProposalModel proposal;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment