File indexing completed on 2024-05-12 05:43:33

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "config-elf-dissector.h"
0019 #include "codenavigatorprinter.h"
0020 #include "codenavigator.h"
0021 
0022 #if HAVE_DWARF
0023 #include <dwarf/dwarfdie.h>
0024 #include <dwarf.h>
0025 #endif
0026 
0027 #include <QFileInfo>
0028 #include <QUrl>
0029 
0030 QString CodeNavigatorPrinter::sourceLocationRichText(DwarfDie* die)
0031 {
0032     QString s;
0033     if (!die)
0034         return s;
0035 
0036 #if HAVE_DWARF
0037     const auto fileName = die->sourceFilePath();
0038     if (fileName.isEmpty())
0039         return s;
0040 
0041     const auto lineNum = die->attribute(DW_AT_decl_line).toInt();
0042     const auto hasCodeNavigation = CodeNavigator::isValid() && QFileInfo(fileName).isAbsolute();
0043 
0044     s += QLatin1String("Source location: ");
0045     if (hasCodeNavigation) {
0046         QUrl url;
0047         url.setScheme(QStringLiteral("code"));
0048         url.setPath(fileName);
0049         url.setFragment(QString::number(lineNum));
0050         s += QLatin1String("<a href=\"");
0051         s += url.toEncoded();
0052         s += QLatin1String("\">");
0053     }
0054     s += fileName;
0055     if (lineNum > 0)
0056         s += ':' + QString::number(lineNum);
0057     if (hasCodeNavigation) {
0058         s += QLatin1String("</a>");
0059     }
0060     s += QLatin1String("<br/>");
0061 #endif
0062 
0063     return s;
0064 }