1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/*
This file is part of the clazy static checker.
Copyright (C) 2017 Sergio Martins <smartins@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef CLAZY_PREPROCESSOR_VISITOR_H
#define CLAZY_PREPROCESSOR_VISITOR_H
// Each check can visit the preprocessor, but doing things per-check can be
// time consuming and we might want to do them only once.
// For example, getting the Qt version can be done once and the result shared
// with all checks
#include "checkbase.h"
#include <clang/Lex/PPCallbacks.h>
#include <llvm/ADT/StringRef.h>
#include <string>
#include <unordered_map>
#include <vector>
namespace clang {
class CompilerInstance;
class SourceManager;
class SourceRange;
class Token;
class MacroDefinition;
class MacroArgs;
class SourceLocation;
}
using uint = unsigned;
class PreProcessorVisitor
: public clang::PPCallbacks
{
PreProcessorVisitor(const PreProcessorVisitor &) = delete;
public:
explicit PreProcessorVisitor(const clang::CompilerInstance &ci);
// Returns for example 050601 (Qt 5.6.1), or -1 if we don't know the version
int qtVersion() const { return m_qtVersion; }
bool isBetweenQtNamespaceMacros(clang::SourceLocation loc);
// Returns true if QT_NO_KEYWORDS is defined
bool isQT_NO_KEYWORDS() const { return m_isQtNoKeywords; }
bool hasInclude(const std::string& fileName, bool IsAngled) const;
clang::SourceLocation endOfIncludeSection() const;
protected:
void MacroExpands(const clang::Token &MacroNameTok, const clang::MacroDefinition &,
clang::SourceRange range, const clang::MacroArgs *) override;
void InclusionDirective (clang::SourceLocation HashLoc, const clang::Token &IncludeTok,
clang::StringRef FileName, bool IsAngled, clang::CharSourceRange FilenameRange,
clazy::OptionalFileEntryRef File, clang::StringRef SearchPath, clang::StringRef RelativePath,
const clang::Module *Imported, clang::SrcMgr::CharacteristicKind FileType) override;
private:
std::string getTokenSpelling(const clang::MacroDefinition &) const;
void updateQtVersion();
void handleQtNamespaceMacro(clang::SourceLocation loc, clang::StringRef name);
const clang::CompilerInstance &m_ci;
int m_qtMajorVersion = -1;
int m_qtMinorVersion = -1;
int m_qtPatchVersion = -1;
int m_qtVersion = -1;
bool m_isQtNoKeywords = false;
// Indexed by FileId, has a list of QT_BEGIN_NAMESPACE/QT_END_NAMESPACE location
std::unordered_map<uint, std::vector<clang::SourceRange>> m_q_namespace_macro_locations;
const clang::SourceManager &m_sm;
struct IncludeInfo {
clang::StringRef fileName;
bool IsAngled;
clang::CharSourceRange filenameRange;
};
std::vector<IncludeInfo> m_includeInfo;
};
#endif
|