1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Dmitry Savchenko
4 ** Copyright (C) 2016 Vasiliy Sorokin
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of Qt Creator.
8 **
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ****************************************************************************/
26 
27 #include "cpptodoitemsscanner.h"
28 
29 #include <cpptools/projectinfo.h>
30 
31 #include <projectexplorer/project.h>
32 
33 #include <cplusplus/TranslationUnit.h>
34 
35 #include <utils/algorithm.h>
36 
37 #include <cctype>
38 
39 namespace Todo {
40 namespace Internal {
41 
CppTodoItemsScanner(const KeywordList & keywordList,QObject * parent)42 CppTodoItemsScanner::CppTodoItemsScanner(const KeywordList &keywordList, QObject *parent) :
43     TodoItemsScanner(keywordList, parent)
44 {
45     CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
46 
47     connect(modelManager, &CppTools::CppModelManager::documentUpdated,
48             this, &CppTodoItemsScanner::documentUpdated, Qt::DirectConnection);
49 
50     setParams(keywordList);
51 }
52 
scannerParamsChanged()53 void CppTodoItemsScanner::scannerParamsChanged()
54 {
55     // We need to rescan everything known to the code model
56     // TODO: It would be nice to only tokenize the source files, not update the code model entirely.
57 
58     CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
59 
60     QSet<QString> filesToBeUpdated;
61     foreach (const CppTools::ProjectInfo &info, modelManager->projectInfos())
62         filesToBeUpdated.unite(Utils::transform<QSet>(info.project().data()->files(ProjectExplorer::Project::SourceFiles),
63                                                 &Utils::FilePath::toString));
64 
65     modelManager->updateSourceFiles(filesToBeUpdated);
66 }
67 
documentUpdated(CPlusPlus::Document::Ptr doc)68 void CppTodoItemsScanner::documentUpdated(CPlusPlus::Document::Ptr doc)
69 {
70     CppTools::CppModelManager *modelManager = CppTools::CppModelManager::instance();
71     if (!modelManager->projectPart(doc->fileName()).isEmpty())
72         processDocument(doc);
73 }
74 
processDocument(CPlusPlus::Document::Ptr doc)75 void CppTodoItemsScanner::processDocument(CPlusPlus::Document::Ptr doc)
76 {
77     QList<TodoItem> itemList;
78     CPlusPlus::TranslationUnit *translationUnit = doc->translationUnit();
79 
80     for (int i = 0; i < translationUnit->commentCount(); ++i) {
81 
82         // Get comment source
83         CPlusPlus::Token token = doc->translationUnit()->commentAt(i);
84         QByteArray source = doc->utf8Source().mid(token.bytesBegin(), token.bytes()).trimmed();
85 
86         if ((token.kind() == CPlusPlus::T_COMMENT) || (token.kind() == CPlusPlus::T_DOXY_COMMENT)) {
87             // Remove trailing "*/"
88             source = source.left(source.length() - 2);
89         }
90 
91         // Process every line of the comment
92         int lineNumber = 0;
93         translationUnit->getPosition(token.utf16charsBegin(), &lineNumber);
94 
95         for (int from = 0, sz = source.size(); from < sz; ++lineNumber) {
96             int to = source.indexOf('\n', from);
97             if (to == -1)
98                 to = sz - 1;
99 
100             const char *start = source.constData() + from;
101             const char *end = source.constData() + to;
102             while (start != end && std::isspace((unsigned char)*start))
103                 ++start;
104             while (start != end && std::isspace((unsigned char)*end))
105                 --end;
106             const int length = end - start + 1;
107             if (length > 0) {
108                 QString commentLine = QString::fromUtf8(start, length);
109                 processCommentLine(doc->fileName(), commentLine, lineNumber, itemList);
110             }
111 
112             from = to + 1;
113         }
114     }
115     emit itemsFetched(doc->fileName(), itemList);
116 }
117 
118 }
119 }
120