File indexing completed on 2024-05-12 04:39:14

0001 /*
0002     SPDX-FileCopyrightText: 2015 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "unsavedfile.h"
0008 
0009 #include <clang-c/Index.h>
0010 
0011 #include <algorithm>
0012 
0013 UnsavedFile::UnsavedFile(const QString& fileName, const QStringList& contents)
0014     : m_fileName(fileName)
0015     , m_contents(contents)
0016 {
0017 }
0018 
0019 CXUnsavedFile UnsavedFile::toClangApi() const
0020 {
0021     if (m_fileNameUtf8.isEmpty()) {
0022         const_cast<UnsavedFile*>(this)->convertToUtf8();
0023     }
0024 
0025     CXUnsavedFile file;
0026     file.Contents = m_contentsUtf8.data();
0027     file.Length = m_contentsUtf8.size();
0028     file.Filename = m_fileNameUtf8.data();
0029 
0030     return file;
0031 }
0032 
0033 void UnsavedFile::convertToUtf8()
0034 {
0035     m_fileNameUtf8 = m_fileName.toUtf8();
0036     m_contentsUtf8.clear();
0037     for (const QString& line : qAsConst(m_contents)) {
0038         m_contentsUtf8 += line.toUtf8() + '\n';
0039     }
0040 }