ClangTools: Replace FilePath::toString

Replace occurrences of FilePath::toString with more sensible alternatives.
Use FilePath capabilities instead of QDir/QFile's where applicable.
Use FilePath instead of QString where it makes sense.

Change-Id: Idc352dbff846c2678f1e3295ac34b3285a711574
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Andrii Semkiv
2024-09-09 16:36:38 +02:00
parent 9af5ec46d8
commit 218a8b7787
11 changed files with 39 additions and 46 deletions

View File

@@ -61,7 +61,7 @@ static Tree *createDirNode(const QString &name, const FilePath &filePath = FileP
static Tree *createFileNode(const FileInfo &fileInfo, bool displayFullPath = false) static Tree *createFileNode(const FileInfo &fileInfo, bool displayFullPath = false)
{ {
auto node = new TreeWithFileInfo; auto node = new TreeWithFileInfo;
node->name = displayFullPath ? fileInfo.file.toString() : fileInfo.file.fileName(); node->name = displayFullPath ? fileInfo.file.toUserOutput() : fileInfo.file.fileName();
node->fullPath = fileInfo.file; node->fullPath = fileInfo.file;
node->info = fileInfo; node->info = fileInfo;

View File

@@ -1250,7 +1250,7 @@ void ClangTool::setState(State state)
QSet<Diagnostic> ClangTool::diagnostics() const QSet<Diagnostic> ClangTool::diagnostics() const
{ {
return Utils::filtered(m_diagnosticModel->diagnostics(), [](const Diagnostic &diagnostic) { return Utils::filtered(m_diagnosticModel->diagnostics(), [](const Diagnostic &diagnostic) {
return ProjectFile::isSource(ProjectFile::classify(diagnostic.location.filePath.toString())); return ProjectFile::isSource(ProjectFile::classify(diagnostic.location.filePath));
}); });
} }

View File

@@ -653,7 +653,7 @@ bool DiagnosticFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &s
continue; continue;
Utils::FilePath filePath = d.filePath; Utils::FilePath filePath = d.filePath;
if (d.filePath.toFileInfo().isRelative()) if (d.filePath.toFileInfo().isRelative())
filePath = m_lastProjectDirectory.pathAppended(filePath.toString()); filePath = m_lastProjectDirectory.resolvePath(filePath);
if (filePath == diag.location.filePath) { if (filePath == diag.location.filePath) {
diagnosticItem->setTextMarkVisible(false); diagnosticItem->setTextMarkVisible(false);
return false; return false;

View File

@@ -129,7 +129,7 @@ public:
Debugger::DiagnosticLocation toDiagnosticLocation() const Debugger::DiagnosticLocation toDiagnosticLocation() const
{ {
FileCache::Item &cacheItem = m_fileCache.item(m_filePath.toString()); FileCache::Item &cacheItem = m_fileCache.item(m_filePath.toUserOutput());
const QByteArray fileContents = cacheItem.fileContents(); const QByteArray fileContents = cacheItem.fileContents();
const char *data = fileContents.data(); const char *data = fileContents.data();

View File

@@ -139,31 +139,27 @@ void ClangToolsProjectSettings::load()
// Read map // Read map
m_useGlobalSettings = map.value(SETTINGS_KEY_USE_GLOBAL_SETTINGS).toBool(); m_useGlobalSettings = map.value(SETTINGS_KEY_USE_GLOBAL_SETTINGS).toBool();
auto toFileName = [](const QString &s) { return Utils::FilePath::fromString(s); }; const QVariantList dirs = map.value(SETTINGS_KEY_SELECTED_DIRS).toList();
const QStringList dirs = map.value(SETTINGS_KEY_SELECTED_DIRS).toStringList(); m_selectedDirs = Utils::transform<QSet>(dirs, Utils::FilePath::fromSettings);
m_selectedDirs = Utils::transform<QSet>(dirs, toFileName);
const QStringList files = map.value(SETTINGS_KEY_SELECTED_FILES).toStringList(); const QVariantList files = map.value(SETTINGS_KEY_SELECTED_FILES).toList();
m_selectedFiles = Utils::transform<QSet>(files, toFileName); m_selectedFiles = Utils::transform<QSet>(files, Utils::FilePath::fromSettings);
const QVariantList list = map.value(SETTINGS_KEY_SUPPRESSED_DIAGS).toList(); const QVariantList list = map.value(SETTINGS_KEY_SUPPRESSED_DIAGS).toList();
for (const QVariant &v : list) { for (const QVariant &v : list) {
const Store diag = storeFromVariant(v); const Store diag = storeFromVariant(v);
const QString fp = diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_FILEPATH).toString(); const auto fp = Utils::FilePath::fromSettings(
diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_FILEPATH));
if (fp.isEmpty()) if (fp.isEmpty())
continue; continue;
const QString message = diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_MESSAGE).toString(); const QString message = diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_MESSAGE).toString();
if (message.isEmpty()) if (message.isEmpty())
continue; continue;
Utils::FilePath fullPath = Utils::FilePath::fromString(fp); const Utils::FilePath fullPath = m_project->projectDirectory().resolvePath(fp);
if (fullPath.toFileInfo().isRelative())
fullPath = m_project->projectDirectory().pathAppended(fp);
if (!fullPath.exists()) if (!fullPath.exists())
continue; continue;
const int uniquifier = diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_UNIQIFIER).toInt(); const int uniquifier = diag.value(SETTINGS_KEY_SUPPRESSED_DIAGS_UNIQIFIER).toInt();
m_suppressedDiagnostics << SuppressedDiagnostic(Utils::FilePath::fromString(fp), m_suppressedDiagnostics << SuppressedDiagnostic(fp, message, uniquifier);
message,
uniquifier);
} }
emit suppressedDiagnosticsChanged(); emit suppressedDiagnosticsChanged();
@@ -178,16 +174,17 @@ void ClangToolsProjectSettings::store()
Store map; Store map;
map.insert(SETTINGS_KEY_USE_GLOBAL_SETTINGS, m_useGlobalSettings); map.insert(SETTINGS_KEY_USE_GLOBAL_SETTINGS, m_useGlobalSettings);
const QStringList dirs = Utils::transform<QList>(m_selectedDirs, &Utils::FilePath::toString); const QVariantList dirs = Utils::transform<QList>(m_selectedDirs, &Utils::FilePath::toSettings);
map.insert(SETTINGS_KEY_SELECTED_DIRS, dirs); map.insert(SETTINGS_KEY_SELECTED_DIRS, dirs);
const QStringList files = Utils::transform<QList>(m_selectedFiles, &Utils::FilePath::toString); const QVariantList files
= Utils::transform<QList>(m_selectedFiles, &Utils::FilePath::toSettings);
map.insert(SETTINGS_KEY_SELECTED_FILES, files); map.insert(SETTINGS_KEY_SELECTED_FILES, files);
QVariantList list; QVariantList list;
for (const SuppressedDiagnostic &diag : std::as_const(m_suppressedDiagnostics)) { for (const SuppressedDiagnostic &diag : std::as_const(m_suppressedDiagnostics)) {
Store diagMap; Store diagMap;
diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_FILEPATH, diag.filePath.toString()); diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_FILEPATH, diag.filePath.toSettings());
diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_MESSAGE, diag.description); diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_MESSAGE, diag.description);
diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_UNIQIFIER, diag.uniquifier); diagMap.insert(SETTINGS_KEY_SUPPRESSED_DIAGS_UNIQIFIER, diag.uniquifier);
list << variantFromStore(diagMap); list << variantFromStore(diagMap);

View File

@@ -154,7 +154,7 @@ FilePath fullPath(const FilePath &executable)
candidate = candidate.withExecutableSuffix(); candidate = candidate.withExecutableSuffix();
} else { } else {
const Environment environment = Environment::systemEnvironment(); const Environment environment = Environment::systemEnvironment();
const FilePath expandedPath = environment.searchInPath(candidate.toString()); const FilePath expandedPath = environment.searchInPath(candidate.fileName());
if (!expandedPath.isEmpty()) if (!expandedPath.isEmpty())
candidate = expandedPath; candidate = expandedPath;
} }

View File

@@ -401,8 +401,7 @@ static void buildTree(ProjectExplorer::Tree *parent,
current->name = node.name; current->name = node.name;
current->isDir = node.children.size(); current->isDir = node.children.size();
if (parent) { if (parent) {
current->fullPath = Utils::FilePath::fromString(parent->fullPath.toString() current->fullPath = parent->fullPath.pathAppended(current->name);
+ current->name);
parent->childDirectories.push_back(current); parent->childDirectories.push_back(current);
} else { } else {
current->fullPath = Utils::FilePath::fromString(current->name); current->fullPath = Utils::FilePath::fromString(current->name);
@@ -413,9 +412,9 @@ static void buildTree(ProjectExplorer::Tree *parent,
} }
static bool needsLink(ProjectExplorer::Tree *node) { static bool needsLink(ProjectExplorer::Tree *node) {
if (node->fullPath.toString() == "clang-analyzer-") if (node->fullPath.path() == "clang-analyzer-")
return true; return true;
return !node->isDir && !node->fullPath.toString().startsWith("clang-analyzer-"); return !node->isDir && !node->fullPath.startsWith("clang-analyzer-");
} }
class BaseChecksTreeModel : public ProjectExplorer::SelectableFilesModel // FIXME: This isn't about files. class BaseChecksTreeModel : public ProjectExplorer::SelectableFilesModel // FIXME: This isn't about files.
@@ -591,7 +590,7 @@ public:
// 'clang-analyzer-' group // 'clang-analyzer-' group
if (node->isDir) if (node->isDir)
return CppEditor::Constants::CLANG_STATIC_ANALYZER_DOCUMENTATION_URL; return CppEditor::Constants::CLANG_STATIC_ANALYZER_DOCUMENTATION_URL;
return clangTidyDocUrl(node->fullPath.toString()); return clangTidyDocUrl(node->fullPath.path());
} }
return BaseChecksTreeModel::data(fullIndex, role); return BaseChecksTreeModel::data(fullIndex, role);
@@ -629,7 +628,7 @@ private:
return false; return false;
auto *node = static_cast<Tree *>(index.internalPointer()); auto *node = static_cast<Tree *>(index.internalPointer());
const QString nodeName = node->fullPath.toString(); const QString nodeName = node->fullPath.path();
if ((check.endsWith("*") && nodeName.startsWith(check.left(check.length() - 1))) if ((check.endsWith("*") && nodeName.startsWith(check.left(check.length() - 1)))
|| (!node->isDir && nodeName == check)) { || (!node->isDir && nodeName == check)) {
result = index; result = index;
@@ -646,7 +645,7 @@ private:
if (root->checked == Qt::Unchecked) if (root->checked == Qt::Unchecked)
return; return;
if (root->checked == Qt::Checked) { if (root->checked == Qt::Checked) {
checks += "," + root->fullPath.toString(); checks += "," + root->fullPath.path();
if (root->isDir) if (root->isDir)
checks += "*"; checks += "*";
return; return;

View File

@@ -208,13 +208,14 @@ void DocumentClangToolRunner::run()
const AnalyzeUnits units{{m_fileInfo, tool}}; const AnalyzeUnits units{{m_fileInfo, tool}};
const auto diagnosticFilter = [mappedPath = vfso().autoSavedFilePath(m_document)]( const auto diagnosticFilter = [mappedPath = vfso().autoSavedFilePath(m_document)](
const FilePath &path) { return path == mappedPath; }; const FilePath &path) { return path == mappedPath; };
const AnalyzeInputData input{tool, const AnalyzeInputData input{
runSettings, tool,
config, runSettings,
m_temporaryDir.path(), config,
env, m_temporaryDir.path(),
vfso().overlayFilePath().toString(), env,
diagnosticFilter}; vfso().overlayFilePath().nativePath(),
diagnosticFilter};
const auto setupHandler = [this, executable](const AnalyzeUnit &) { const auto setupHandler = [this, executable](const AnalyzeUnit &) {
return !m_document->isModified() || isVFSOverlaySupported(executable); return !m_document->isModified() || isVFSOverlaySupported(executable);
}; };
@@ -317,7 +318,7 @@ bool DocumentClangToolRunner::isSuppressed(const Diagnostic &diagnostic) const
return false; return false;
FilePath filePath = suppressed.filePath; FilePath filePath = suppressed.filePath;
if (filePath.toFileInfo().isRelative()) if (filePath.toFileInfo().isRelative())
filePath = m_lastProjectDirectory.pathAppended(filePath.toString()); filePath = m_lastProjectDirectory.resolvePath(filePath);
return filePath == diagnostic.location.filePath; return filePath == diagnostic.location.filePath;
}; };
return Utils::anyOf(m_suppressed, equalsSuppressed); return Utils::anyOf(m_suppressed, equalsSuppressed);

View File

@@ -42,13 +42,12 @@ static Range toRange(const QTextDocument *doc, DiagnosticRange locations)
void ClangToolQuickFixOperation::perform() void ClangToolQuickFixOperation::perform()
{ {
TextEditor::PlainRefactoringFileFactory changes; TextEditor::PlainRefactoringFileFactory changes;
QMap<QString, TextEditor::RefactoringFilePtr> refactoringFiles; QMap<Utils::FilePath, TextEditor::RefactoringFilePtr> refactoringFiles;
for (const ExplainingStep &step : m_diagnostic.explainingSteps) { for (const ExplainingStep &step : m_diagnostic.explainingSteps) {
if (!step.isFixIt) if (!step.isFixIt)
continue; continue;
TextEditor::RefactoringFilePtr &refactoringFile TextEditor::RefactoringFilePtr &refactoringFile = refactoringFiles[step.location.filePath];
= refactoringFiles[step.location.filePath.toString()];
if (refactoringFile.isNull()) if (refactoringFile.isNull())
refactoringFile = changes.file(step.location.filePath); refactoringFile = changes.file(step.location.filePath);
Utils::ChangeSet changeSet = refactoringFile->changeSet(); Utils::ChangeSet changeSet = refactoringFile->changeSet();

View File

@@ -164,9 +164,8 @@ static FilePath queryResourceDir(const FilePath &clangToolPath)
[&clangToolPath](const QString &stdOut, const QString &) -> std::optional<FilePath> { [&clangToolPath](const QString &stdOut, const QString &) -> std::optional<FilePath> {
QString output = stdOut; QString output = stdOut;
QTextStream stream(&output); QTextStream stream(&output);
const QString path = clangToolPath.parentDir().parentDir() const FilePath filePath
.pathAppended(stream.readLine()).toString(); = clangToolPath.parentDir().parentDir().pathAppended(stream.readLine()).cleanPath();
const auto filePath = FilePath::fromUserInput(QDir::cleanPath(path));
if (filePath.exists()) if (filePath.exists())
return filePath; return filePath;
return {}; return {};

View File

@@ -26,9 +26,6 @@ VirtualFileSystemOverlay::VirtualFileSystemOverlay(const QString &rootPattern)
void VirtualFileSystemOverlay::update() void VirtualFileSystemOverlay::update()
{ {
overlayFilePath().removeRecursively(); overlayFilePath().removeRecursively();
QFile overlayFile(m_overlayFilePath.toString());
if (!overlayFile.open(QFile::ReadWrite))
return;
std::map<Utils::FilePath, QList<Core::IDocument *>> documentRoots; std::map<Utils::FilePath, QList<Core::IDocument *>> documentRoots;
const QList<Core::IDocument *> &modifiedDocuments = Core::DocumentManager::modifiedDocuments(); const QList<Core::IDocument *> &modifiedDocuments = Core::DocumentManager::modifiedDocuments();
QHash<Core::IDocument *, AutoSavedPath> newSaved; QHash<Core::IDocument *, AutoSavedPath> newSaved;
@@ -87,9 +84,10 @@ void VirtualFileSystemOverlay::update()
main["roots"] = jsonRoots; main["roots"] = jsonRoots;
QJsonDocument overlay(main); QJsonDocument overlay(main);
if (!overlayFile.write(overlay.toJson(QJsonDocument::Compact))) const Utils::expected_str<qint64> res = m_overlayFilePath.writeFileContents(
overlay.toJson(QJsonDocument::Compact));
if (!res)
qCDebug(LOG) << "failed to write vfso to " << m_overlayFilePath; qCDebug(LOG) << "failed to write vfso to " << m_overlayFilePath;
overlayFile.close();
} }
Utils::FilePath VirtualFileSystemOverlay::overlayFilePath() const { return m_overlayFilePath; } Utils::FilePath VirtualFileSystemOverlay::overlayFilePath() const { return m_overlayFilePath; }