2016-11-15 15:38:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 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 "clangtool.h"
|
|
|
|
|
|
2018-10-18 17:22:47 +02:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// use std::filesystem::path if it is supported by all compilers
|
|
|
|
|
|
2016-11-30 15:29:36 +01:00
|
|
|
template <typename String>
|
|
|
|
|
String toNativePath(String &&path)
|
2016-11-15 15:38:12 +01:00
|
|
|
{
|
2016-11-16 16:04:59 +01:00
|
|
|
#ifdef _WIN32
|
2016-11-15 15:38:12 +01:00
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return std::move(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ClangTool::addFile(std::string &&directory,
|
2017-08-02 16:00:55 +02:00
|
|
|
std::string &&fileName,
|
|
|
|
|
std::string &&content,
|
|
|
|
|
std::vector<std::string> &&commandLine)
|
2016-11-15 15:38:12 +01:00
|
|
|
{
|
2017-07-24 14:55:51 +02:00
|
|
|
m_fileContents.emplace_back(toNativePath(std::move(directory)),
|
2018-12-17 12:06:57 +01:00
|
|
|
std::move(fileName),
|
|
|
|
|
std::move(content),
|
|
|
|
|
std::move(commandLine));
|
2016-11-15 15:38:12 +01:00
|
|
|
|
2017-07-24 14:55:51 +02:00
|
|
|
const auto &fileContent = m_fileContents.back();
|
2016-11-15 15:38:12 +01:00
|
|
|
|
2017-07-24 14:55:51 +02:00
|
|
|
m_compilationDatabase.addFile(fileContent.directory, fileContent.fileName, fileContent.commandLine);
|
|
|
|
|
m_sourceFilePaths.push_back(fileContent.filePath);
|
2016-11-15 15:38:12 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-22 14:21:01 +01:00
|
|
|
void ClangTool::addFiles(const FilePaths &filePaths, const Utils::SmallStringVector &arguments)
|
|
|
|
|
{
|
|
|
|
|
for (const FilePath &filePath : filePaths) {
|
|
|
|
|
std::vector<std::string> commandLine(arguments.begin(), arguments.end());
|
|
|
|
|
commandLine.push_back(std::string(filePath.name()));
|
|
|
|
|
|
|
|
|
|
addFile(filePath.directory(),
|
|
|
|
|
filePath.name(),
|
|
|
|
|
{},
|
|
|
|
|
std::move(commandLine));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
template <typename Container>
|
|
|
|
|
void ClangTool::addFiles(const Container &filePaths,
|
2016-11-23 13:13:38 +01:00
|
|
|
const Utils::SmallStringVector &arguments)
|
|
|
|
|
{
|
2017-02-01 13:43:28 +01:00
|
|
|
for (const typename Container::value_type &filePath : filePaths) {
|
2016-11-23 13:13:38 +01:00
|
|
|
auto found = std::find(filePath.rbegin(), filePath.rend(), '/');
|
|
|
|
|
|
|
|
|
|
auto fileNameBegin = found.base();
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> commandLine(arguments.begin(), arguments.end());
|
2017-08-29 12:54:10 +02:00
|
|
|
commandLine.push_back(std::string(filePath));
|
2016-11-23 13:13:38 +01:00
|
|
|
|
|
|
|
|
addFile({filePath.begin(), std::prev(fileNameBegin)},
|
|
|
|
|
{fileNameBegin, filePath.end()},
|
|
|
|
|
{},
|
|
|
|
|
std::move(commandLine));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
template
|
|
|
|
|
void ClangTool::addFiles<Utils::SmallStringVector>(const Utils::SmallStringVector &filePaths,
|
|
|
|
|
const Utils::SmallStringVector &arguments);
|
|
|
|
|
template
|
|
|
|
|
void ClangTool::addFiles<Utils::PathStringVector>(const Utils::PathStringVector &filePaths,
|
|
|
|
|
const Utils::SmallStringVector &arguments);
|
|
|
|
|
|
|
|
|
|
void ClangTool::addUnsavedFiles(const V2::FileContainers &unsavedFiles)
|
2016-11-30 15:29:36 +01:00
|
|
|
{
|
2017-07-24 14:55:51 +02:00
|
|
|
m_unsavedFileContents.reserve(m_unsavedFileContents.size() + unsavedFiles.size());
|
2016-11-30 15:29:36 +01:00
|
|
|
|
2017-02-01 13:43:28 +01:00
|
|
|
auto convertToUnsavedFileContent = [] (const V2::FileContainer &unsavedFile) {
|
2018-04-08 01:20:42 +03:00
|
|
|
return UnsavedFileContent{toNativePath(unsavedFile.filePath.path().clone()),
|
|
|
|
|
unsavedFile.unsavedFileContent.clone()};
|
2016-11-30 15:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::transform(unsavedFiles.begin(),
|
|
|
|
|
unsavedFiles.end(),
|
2017-07-24 14:55:51 +02:00
|
|
|
std::back_inserter(m_unsavedFileContents),
|
2016-11-30 15:29:36 +01:00
|
|
|
convertToUnsavedFileContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
2017-07-03 11:12:00 +02:00
|
|
|
template <typename String>
|
|
|
|
|
llvm::StringRef toStringRef(const String &string)
|
2016-11-30 15:29:36 +01:00
|
|
|
{
|
|
|
|
|
return llvm::StringRef(string.data(), string.size());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
clang::tooling::ClangTool ClangTool::createTool() const
|
|
|
|
|
{
|
2017-07-24 14:55:51 +02:00
|
|
|
clang::tooling::ClangTool tool(m_compilationDatabase, m_sourceFilePaths);
|
2016-11-15 15:38:12 +01:00
|
|
|
|
2017-07-24 14:55:51 +02:00
|
|
|
for (const auto &fileContent : m_fileContents) {
|
2016-11-15 15:38:12 +01:00
|
|
|
if (!fileContent.content.empty())
|
|
|
|
|
tool.mapVirtualFile(fileContent.filePath, fileContent.content);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-24 14:55:51 +02:00
|
|
|
for (const auto &unsavedFileContent : m_unsavedFileContents)
|
2016-11-30 15:29:36 +01:00
|
|
|
tool.mapVirtualFile(toStringRef(unsavedFileContent.filePath),
|
|
|
|
|
toStringRef(unsavedFileContent.content));
|
|
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
tool.mapVirtualFile("/dummyFile", "#pragma once");
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
return tool;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 12:06:57 +01:00
|
|
|
clang::tooling::ClangTool ClangTool::createOutputTool() const
|
|
|
|
|
{
|
|
|
|
|
clang::tooling::ClangTool tool = createTool();
|
|
|
|
|
|
|
|
|
|
tool.clearArgumentsAdjusters();
|
|
|
|
|
|
|
|
|
|
return tool;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
} // namespace ClangBackEnd
|