1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #pragma once
27 
28 #include "clangfileinfo.h"
29 #include "clangtoolssettings.h"
30 
31 #include <cpptools/clangdiagnosticconfig.h>
32 #include <cpptools/projectinfo.h>
33 #include <projectexplorer/runcontrol.h>
34 #include <utils/environment.h>
35 #include <utils/temporarydirectory.h>
36 
37 #include <QElapsedTimer>
38 #include <QFutureInterface>
39 #include <QSet>
40 #include <QStringList>
41 
42 namespace ClangTools {
43 namespace Internal {
44 
45 class ClangToolRunner;
46 class ProjectBuilder;
47 
48 struct AnalyzeUnit {
49     AnalyzeUnit(const FileInfo &fileInfo,
50                 const Utils::FilePath &clangResourceDir,
51                 const QString &clangVersion);
52 
53     QString file;
54     QStringList arguments; // without file itself and "-o somePath"
55 };
56 using AnalyzeUnits = QList<AnalyzeUnit>;
57 
58 using RunnerCreator = std::function<ClangToolRunner*()>;
59 
60 struct QueueItem {
61     AnalyzeUnit unit;
62     RunnerCreator runnerCreator;
63 };
64 using QueueItems = QList<QueueItem>;
65 
66 class ClangToolRunWorker : public ProjectExplorer::RunWorker
67 {
68     Q_OBJECT
69 
70 public:
71     ClangToolRunWorker(ProjectExplorer::RunControl *runControl,
72                        const RunSettings &runSettings,
73                        const CppTools::ClangDiagnosticConfig &diagnosticConfig,
74                        const FileInfos &fileInfos,
75                        bool buildBeforeAnalysis);
76 
filesAnalyzed()77     int filesAnalyzed() const { return m_filesAnalyzed.size(); }
filesNotAnalyzed()78     int filesNotAnalyzed() const { return m_filesNotAnalyzed.size(); }
totalFilesToAnalyze()79     int totalFilesToAnalyze() const { return int(m_fileInfos.size()); }
80 
81 signals:
82     void buildFailed();
83     void runnerFinished();
84     void startFailed();
85 
86 protected:
87     void onRunnerFinishedWithSuccess(const QString &filePath);
88     void onRunnerFinishedWithFailure(const QString &errorMessage, const QString &errorDetails);
89 
90 private:
91     void start() final;
92     void stop() final;
93 
94     QList<RunnerCreator> runnerCreators();
95     template <class T> ClangToolRunner *createRunner();
96 
97     AnalyzeUnits unitsToAnalyze(const Utils::FilePath &clangIncludeDir,
98                                 const QString &clangVersion);
99     void analyzeNextFile();
100 
101     void handleFinished();
102 
103     void onProgressCanceled();
104     void updateProgressValue();
105 
106     void finalize();
107 
108 private:
109     RunSettings m_runSettings;
110     CppTools::ClangDiagnosticConfig m_diagnosticConfig;
111     FileInfos m_fileInfos;
112 
113     ProjectBuilder *m_projectBuilder = nullptr;
114     Utils::Environment m_environment;
115     Utils::TemporaryDirectory m_temporaryDir;
116 
117     CppTools::ProjectInfo m_projectInfoBeforeBuild;
118     CppTools::ProjectInfo m_projectInfo;
119     QString m_targetTriple;
120     Utils::Id m_toolChainType;
121 
122     QFutureInterface<void> m_progress;
123     QueueItems m_queue;
124     QSet<Utils::FilePath> m_projectFiles;
125     QSet<ClangToolRunner *> m_runners;
126     int m_initialQueueSize = 0;
127     QSet<QString> m_filesAnalyzed;
128     QSet<QString> m_filesNotAnalyzed;
129 
130     QElapsedTimer m_elapsed;
131 };
132 
133 } // namespace Internal
134 } // namespace ClangTools
135