File indexing completed on 2024-04-14 05:32:08

0001 /*
0002     Copyright (C) Sérgio Martins <iamsergio@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef CLAZY_BODYRANGE_H
0008 #define CLAZY_BODYRANGE_H
0009 
0010 #include <clang/AST/Stmt.h>
0011 #include <clang/Basic/SourceLocation.h>
0012 #include <clang/Basic/SourceManager.h>
0013 
0014 struct StmtBodyRange {
0015     clang::Stmt *body = nullptr;
0016     const clang::SourceManager *const sm = nullptr;
0017     const clang::SourceLocation searchUntilLoc; // We don't search after this point
0018 
0019     explicit StmtBodyRange(clang::Stmt *body, const clang::SourceManager *sm = nullptr, clang::SourceLocation searchUntilLoc = {})
0020         : body(body)
0021         , sm(sm)
0022         , searchUntilLoc(searchUntilLoc)
0023     {
0024     }
0025 
0026     bool isValid() const
0027     {
0028         return body != nullptr;
0029     }
0030 
0031     bool isOutsideRange(clang::Stmt *stmt) const
0032     {
0033         return isOutsideRange(stmt ? stmt->getBeginLoc() : clang::SourceLocation());
0034     }
0035 
0036     bool isOutsideRange(clang::SourceLocation loc) const
0037     {
0038         if (loc.isInvalid()) {
0039             return true;
0040         }
0041 
0042         if (!sm || searchUntilLoc.isInvalid()) {
0043             return false;
0044         }
0045 
0046         return sm->isBeforeInSLocAddrSpace(searchUntilLoc, loc);
0047     }
0048 };
0049 
0050 #endif