File indexing completed on 2024-04-28 13:34:29
0001 /* 0002 SPDX-FileCopyrightText: 2016 Sergio Martins <smartins@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef CLAZY_MACRO_UTILS_H 0008 #define CLAZY_MACRO_UTILS_H 0009 0010 #include "clazy_stl.h" 0011 0012 #include <clang/AST/ASTContext.h> 0013 #include <clang/Basic/SourceLocation.h> 0014 #include <clang/Frontend/CompilerInstance.h> 0015 #include <clang/Lex/Lexer.h> 0016 #include <clang/Lex/PreprocessorOptions.h> 0017 0018 #include <vector> 0019 0020 namespace clang 0021 { 0022 class CompilerInstance; 0023 class SourceLocation; 0024 } 0025 0026 namespace clazy 0027 { 0028 /** 0029 * Returns true is macroName was defined via compiler invocation argument. 0030 * Like $ gcc -Dfoo main.cpp 0031 */ 0032 inline bool isPredefined(const clang::PreprocessorOptions &ppOpts, const llvm::StringRef ¯oName) 0033 { 0034 const auto ¯os = ppOpts.Macros; 0035 0036 for (const auto ¯o : macros) { 0037 if (macro.first == macroName) { 0038 return true; 0039 } 0040 } 0041 0042 return false; 0043 } 0044 0045 /** 0046 * Returns true if the source location loc is inside a macro named macroName. 0047 */ 0048 inline bool isInMacro(const clang::ASTContext *context, clang::SourceLocation loc, const llvm::StringRef ¯oName) 0049 { 0050 if (loc.isValid() && loc.isMacroID()) { 0051 llvm::StringRef macro = clang::Lexer::getImmediateMacroName(loc, context->getSourceManager(), context->getLangOpts()); 0052 return macro == macroName; 0053 } 0054 0055 return false; 0056 } 0057 0058 /** 0059 * Returns true if the source location loc is inside any of the specified macros. 0060 */ 0061 inline bool isInAnyMacro(const clang::ASTContext *context, clang::SourceLocation loc, const std::vector<llvm::StringRef> ¯oNames) 0062 { 0063 return clazy::any_of(macroNames, [context, loc](const llvm::StringRef ¯oName) { 0064 return isInMacro(context, loc, macroName); 0065 }); 0066 } 0067 0068 } 0069 0070 #endif