SourceLoader.cpp 4.9 KB
Newer Older
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
1
#include "SourceLoader.h"
2
#include "Utils.h"
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
3 4 5 6 7 8 9 10 11 12 13 14
#include <limits.h>

enum
{
    ADDITIONAL_LINES = 10,
    MAX_FILE_SIZE = 256 * 1024,
};

SourceLoader::SourceLoader(uint32_t totalSamples, const FileProfile& fileProfile)
    : totalSamples(totalSamples)
    , fileProfile(fileProfile)
{
15 16 17 18
    QSettings settings(GetSettingsFile(), QSettings::IniFormat);
    vs2013 = QDir::toNativeSeparators(settings.value("Preferences/VS2013", QString()).toString());
    vs2015 = QDir::toNativeSeparators(settings.value("Preferences/VS2015", QString()).toString());
    sdk10 = QDir::toNativeSeparators(settings.value("Preferences/SDK10", QString()).toString());
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}

SourceLoader::~SourceLoader()
{
}

SymbolPtr SourceLoader::findSymbol(const QString& fname, int line) const
{
    auto& samples = fileProfile[fname];
    auto it = samples.lineToSymbol.find(line);
    if (it == samples.lineToSymbol.end())
    {
        return SymbolPtr();
    }
    else
    {
        return it.value();
    }
}

QFuture<LoadResult> SourceLoader::load(const QString& fname, int lineFrom, int lineTo)
{
    return QtConcurrent::run([=]()
    {
        LoadResult result;

        if (fname.isEmpty())
        {
            result.source = QString("[source file not available]");
            result.loaded = false;
            return result;
        }

        QString fname2 = fname;
53
        if (fname.startsWith("f:\\dd\\vctools\\crt\\crtw32\\") && !vs2013.isEmpty())
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
54
        {
55
            QString path = vs2013 + "\\crt\\src\\";
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
56 57 58 59 60 61
            if (QFileInfo(path + QFileInfo(fname).fileName()).isFile())
            {
                fname2 = path + QFileInfo(fname).fileName();
            }
            else
            {
62
                path = vs2013 + "\\crt\\src\\intel\\";
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
63 64 65 66 67 68
                if (QFileInfo(path + QFileInfo(fname).fileName()).isFile())
                {
                    fname2 = path + QFileInfo(fname).fileName();
                }
            }
        }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        else if (fname.startsWith("f:\\dd\\vctools\\crt\\vcstartup\\src\\startup\\") && !vs2015.isEmpty())
        {
            QString path = vs2015 + "\\crt\\src\\vcruntime\\";
            if (QFileInfo(path + QFileInfo(fname).fileName()).isFile())
            {
                fname2 = path + QFileInfo(fname).fileName();
            }
        }
        else if (fname.startsWith("d:\\th\\minkernel\\crts\\ucrt\\src\\appcrt\\") && !sdk10.isEmpty())
        {
            QString path = sdk10 + "\\ucrt\\" + fname.right(fname.length() - QLatin1String("d:\\th\\minkernel\\crts\\ucrt\\src\\appcrt\\").size());
            if (QFileInfo(path).isFile())
            {
                fname2 = path;
            }
        }
Mārtiņš Možeiko's avatar
Mārtiņš Možeiko committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

        QFile file(fname2);
        if (!file.exists())
        {
            result.source = QString("[source file doesn't exist - %1]").arg(fname);
            result.loaded = false;
            return result;
        }

        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            result.source = QString("[error opening file - %1]").arg(file.errorString());
            result.loaded = false;
            return result;
        }

        int loadLineFrom = lineFrom;
        int loadLineTo = lineTo;
        if (file.size() < MAX_FILE_SIZE)
        {
            result.loadedFileName = fname;
            loadLineFrom = ADDITIONAL_LINES;
            loadLineTo = INT_MAX - 1 - ADDITIONAL_LINES;
        }
        else
        {
            result.loadedFileName = QString("%[email protected]%[email protected]%3").arg(fname).arg(lineFrom).arg(lineTo);
        }
        loadLineFrom = qMax(1, loadLineFrom - ADDITIONAL_LINES);
        loadLineTo = qMin(INT_MAX - 1, loadLineTo + ADDITIONAL_LINES);

        result.loadedFrom = loadLineFrom;

        QStringList lines;
        QStringList percents;

        auto prof = fileProfile.find(fname);
        int lineNr = 1;

        bool needEmptyLine = false;

        QTextStream stream(&file);
        for (;;)
        {
            QString line = stream.readLine();
            if (line.isNull())
            {
                break;
            }

            if (lineNr >= loadLineFrom && lineNr <= loadLineTo)
            {
                if (prof != fileProfile.end() && prof->perLine.contains(lineNr))
                {
                    uint32_t samples = prof->perLine[lineNr];
                    percents.append(QString("%1%").arg(samples * 100.0 / totalSamples, 0, 'f', 2));
                }
                else
                {
                    percents.append(QString());
                }

                if (needEmptyLine)
                {
                    if (!line.trimmed().isEmpty())
                    {
                        lines.append(QString::null);
                    }
                    needEmptyLine = false;
                }

                lines.append(line);
            }

            lineNr++;
        }

        result.percents = percents;
        result.source = lines.join("\n");
        result.loaded = true;
        return result;
    });
}