/* SPDX-FileCopyrightText: 2016 Sergio Martins SPDX-License-Identifier: LGPL-2.0-or-later */ #ifndef CLAZY_MACRO_UTILS_H #define CLAZY_MACRO_UTILS_H #include "clazy_stl.h" #include #include #include #include #include #include namespace clang { class CompilerInstance; class SourceLocation; } namespace clazy { /** * Returns true is macroName was defined via compiler invocation argument. * Like $ gcc -Dfoo main.cpp */ inline bool isPredefined(const clang::PreprocessorOptions &ppOpts, const llvm::StringRef ¯oName) { const auto ¯os = ppOpts.Macros; for (const auto ¯o : macros) { if (macro.first == macroName) { return true; } } return false; } /** * Returns true if the source location loc is inside a macro named macroName. */ inline bool isInMacro(const clang::ASTContext *context, clang::SourceLocation loc, const llvm::StringRef ¯oName) { if (loc.isValid() && loc.isMacroID()) { llvm::StringRef macro = clang::Lexer::getImmediateMacroName(loc, context->getSourceManager(), context->getLangOpts()); return macro == macroName; } return false; } /** * Returns true if the source location loc is inside any of the specified macros. */ inline bool isInAnyMacro(const clang::ASTContext *context, clang::SourceLocation loc, const std::vector ¯oNames) { return std::ranges::any_of(macroNames, [context, loc](const llvm::StringRef ¯oName) { return isInMacro(context, loc, macroName); }); } } #endif