| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef CLAZY_PREPROCESSOR_VISITOR_H |
| 8 |
#define CLAZY_PREPROCESSOR_VISITOR_H |
| 9 |
|
| 10 |
// Each check can visit the preprocessor, but doing things per-check can be |
| 11 |
// time consuming and we might want to do them only once. |
| 12 |
// For example, getting the Qt version can be done once and the result shared |
| 13 |
// with all checks |
| 14 |
|
| 15 |
#include "checkbase.h" |
| 16 |
|
| 17 |
#include <clang/Lex/PPCallbacks.h> |
| 18 |
#include <llvm/ADT/StringRef.h> |
| 19 |
|
| 20 |
#include <string> |
| 21 |
#include <unordered_map> |
| 22 |
#include <vector> |
| 23 |
|
| 24 |
namespace clang |
| 25 |
{ |
| 26 |
class CompilerInstance; |
| 27 |
class SourceManager; |
| 28 |
class SourceRange; |
| 29 |
class Token; |
| 30 |
class MacroDefinition; |
| 31 |
class MacroArgs; |
| 32 |
class SourceLocation; |
| 33 |
} |
| 34 |
|
| 35 |
using uint = unsigned; |
| 36 |
|
| 37 |
class PreProcessorVisitor : public clang::PPCallbacks |
| 38 |
{ |
| 39 |
PreProcessorVisitor(const PreProcessorVisitor &) = delete; |
| 40 |
|
| 41 |
public: |
| 42 |
explicit PreProcessorVisitor(clang::Preprocessor &pi); |
| 43 |
|
| 44 |
// Returns for example 050601 (Qt 5.6.1), or -1 if we don't know the version |
| 45 |
int qtVersion() const |
| 46 |
{ |
| 47 |
return m_qtVersion; |
| 48 |
} |
| 49 |
|
| 50 |
bool isBetweenQtNamespaceMacros(clang::SourceLocation loc); |
| 51 |
|
| 52 |
// Returns true if QT_NO_KEYWORDS is defined |
| 53 |
bool isQT_NO_KEYWORDS() const |
| 54 |
{ |
| 55 |
return m_isQtNoKeywords; |
| 56 |
} |
| 57 |
|
| 58 |
bool hasInclude(const std::string &fileName, bool IsAngled) const; |
| 59 |
clang::SourceLocation endOfIncludeSection() const; |
| 60 |
|
| 61 |
protected: |
| 62 |
void MacroExpands(const clang::Token &MacroNameTok, const clang::MacroDefinition &, clang::SourceRange range, const clang::MacroArgs *) override; |
| 63 |
void InclusionDirective(clang::SourceLocation HashLoc, |
| 64 |
const clang::Token &IncludeTok, |
| 65 |
clang::StringRef FileName, |
| 66 |
bool IsAngled, |
| 67 |
clang::CharSourceRange FilenameRange, |
| 68 |
clazy::OptionalFileEntryRef File, |
| 69 |
clang::StringRef SearchPath, |
| 70 |
clang::StringRef RelativePath, |
| 71 |
const clang::Module *SuggestedModule, |
| 72 |
bool ModuleImported, |
| 73 |
clang::SrcMgr::CharacteristicKind FileType) override; |
| 74 |
|
| 75 |
private: |
| 76 |
std::string getTokenSpelling(const clang::MacroDefinition &) const; |
| 77 |
void updateQtVersion(); |
| 78 |
void handleQtNamespaceMacro(clang::SourceLocation loc, clang::StringRef name); |
| 79 |
|
| 80 |
int m_qtMajorVersion = -1; |
| 81 |
int m_qtMinorVersion = -1; |
| 82 |
int m_qtPatchVersion = -1; |
| 83 |
int m_qtVersion = -1; |
| 84 |
bool m_isQtNoKeywords = false; |
| 85 |
|
| 86 |
// Indexed by FileId, has a list of QT_BEGIN_NAMESPACE/QT_END_NAMESPACE location |
| 87 |
std::unordered_map<uint, std::vector<clang::SourceRange>> m_q_namespace_macro_locations; |
| 88 |
const clang::Preprocessor &m_pp; |
| 89 |
|
| 90 |
struct IncludeInfo { |
| 91 |
clang::StringRef fileName; |
| 92 |
bool IsAngled; |
| 93 |
clang::CharSourceRange filenameRange; |
| 94 |
}; |
| 95 |
|
| 96 |
std::vector<IncludeInfo> m_includeInfo; |
| 97 |
}; |
| 98 |
|
| 99 |
#endif |