ClangRefactoring: Fix PCH build

We have to generate a PCH which is not checked by size or data. For that
we have to remap the file so it get the overridden flag. If that flat is
set no checks will be performed.

Task-number: QTCREATORBUG-21958
Change-Id: Id9bff91b3ab64526dc109eb46d4a21766456abe5
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2019-02-11 17:17:06 +01:00
parent 846ed7c50d
commit 406336bab7
3 changed files with 47 additions and 11 deletions

View File

@@ -27,15 +27,49 @@
#include <clang/Tooling/Tooling.h>
#include "clang/Frontend/FrontendActions.h"
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/Lex/PreprocessorOptions.h>
namespace ClangBackEnd {
class GeneratePCHAction final : public clang::GeneratePCHAction
{
public:
GeneratePCHAction(llvm::StringRef filePath, llvm::StringRef fileContent)
: m_filePath(filePath)
, m_fileContent(fileContent)
{}
bool BeginInvocation(clang::CompilerInstance &compilerInstance) override
{
compilerInstance.getPreprocessorOpts().DisablePCHValidation = true;
std::unique_ptr<llvm::MemoryBuffer> Input = llvm::MemoryBuffer::getMemBuffer(m_fileContent);
compilerInstance.getPreprocessorOpts().addRemappedFile(m_filePath, Input.release());
return clang::GeneratePCHAction::BeginSourceFileAction(compilerInstance);
}
private:
llvm::StringRef m_filePath;
llvm::StringRef m_fileContent;
};
class GeneratePCHActionFactory final : public clang::tooling::FrontendActionFactory
{
public:
GeneratePCHActionFactory(llvm::StringRef filePath, llvm::StringRef fileContent)
: m_filePath(filePath)
, m_fileContent(fileContent)
{}
clang::FrontendAction *create() override
{
return new clang::GeneratePCHAction;
return new GeneratePCHAction{m_filePath, m_fileContent};
}
private:
llvm::StringRef m_filePath;
llvm::StringRef m_fileContent;
};
} // namespace ClangBackEnd

View File

@@ -72,16 +72,20 @@ Utils::SmallString PchCreator::generatePchIncludeFileContent(const FilePathIds &
return fileContent;
}
bool PchCreator::generatePch()
bool PchCreator::generatePch(NativeFilePathView path, Utils::SmallStringView content)
{
clang::tooling::ClangTool tool = m_clangTool.createOutputTool();
auto action = std::make_unique<GeneratePCHActionFactory>();
NativeFilePath headerFilePath{m_environment.pchBuildDirectory().toStdString(), "dummy.h"};
auto action = std::make_unique<GeneratePCHActionFactory>(llvm::StringRef{path.data(),
path.size()},
llvm::StringRef{content.data(),
content.size()});
return tool.run(action.get()) != 1;
}
FilePath PchCreator::generatePchFilePath() const
{
std::uniform_int_distribution<std::uint_fast64_t> distribution(
@@ -113,12 +117,10 @@ void PchCreator::generatePch(PchTask &&pchTask)
auto pchOutputPath = generatePchFilePath();
FilePath headerFilePath{m_environment.pchBuildDirectory().toStdString(), "dummy.h"};
Utils::SmallStringVector commandLine = generateClangCompilerArguments(pchTask,
pchOutputPath);
Utils::SmallStringVector commandLine = generateClangCompilerArguments(pchTask, pchOutputPath);
m_clangTool.addFile(std::move(headerFilePath), std::move(content), std::move(commandLine));
bool success = generatePch();
m_clangTool.addFile(std::move(headerFilePath), content.clone(), std::move(commandLine));
bool success = generatePch(NativeFilePath{headerFilePath}, content);
m_projectPartPch.projectPartId = pchTask.projectPartId();

View File

@@ -81,7 +81,7 @@ public:
const FilePathCaching &filePathCache();
Utils::SmallString generatePchIncludeFileContent(const FilePathIds &includeIds) const;
bool generatePch();
bool generatePch(NativeFilePathView path, Utils::SmallStringView content);
FilePath generatePchFilePath() const;
static Utils::SmallStringVector generateClangCompilerArguments(const PchTask &pchTask,