Home | History | Annotate | Line # | Download | only in AST
      1 //===--- CurrentSourceLocExprScope.h ----------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines types used to track the current context needed to evaluate
     11 //  a SourceLocExpr.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_AST_CURRENT_SOURCE_LOC_EXPR_SCOPE_H
     16 #define LLVM_CLANG_AST_CURRENT_SOURCE_LOC_EXPR_SCOPE_H
     17 
     18 #include <cassert>
     19 
     20 namespace clang {
     21 class Expr;
     22 
     23 /// Represents the current source location and context used to determine the
     24 /// value of the source location builtins (ex. __builtin_LINE), including the
     25 /// context of default argument and default initializer expressions.
     26 class CurrentSourceLocExprScope {
     27   /// The CXXDefaultArgExpr or CXXDefaultInitExpr we're currently evaluating.
     28   const Expr *DefaultExpr = nullptr;
     29 
     30 public:
     31   /// A RAII style scope guard used for tracking the current source
     32   /// location and context as used by the source location builtins
     33   /// (ex. __builtin_LINE).
     34   class SourceLocExprScopeGuard;
     35 
     36   const Expr *getDefaultExpr() const { return DefaultExpr; }
     37 
     38   explicit CurrentSourceLocExprScope() = default;
     39 
     40 private:
     41   explicit CurrentSourceLocExprScope(const Expr *DefaultExpr)
     42       : DefaultExpr(DefaultExpr) {}
     43 
     44   CurrentSourceLocExprScope(CurrentSourceLocExprScope const &) = default;
     45   CurrentSourceLocExprScope &
     46   operator=(CurrentSourceLocExprScope const &) = default;
     47 };
     48 
     49 class CurrentSourceLocExprScope::SourceLocExprScopeGuard {
     50 public:
     51   SourceLocExprScopeGuard(const Expr *DefaultExpr,
     52                           CurrentSourceLocExprScope &Current)
     53       : Current(Current), OldVal(Current), Enable(false) {
     54     assert(DefaultExpr && "the new scope should not be empty");
     55     if ((Enable = (Current.getDefaultExpr() == nullptr)))
     56       Current = CurrentSourceLocExprScope(DefaultExpr);
     57   }
     58 
     59   ~SourceLocExprScopeGuard() {
     60     if (Enable)
     61       Current = OldVal;
     62   }
     63 
     64 private:
     65   SourceLocExprScopeGuard(SourceLocExprScopeGuard const &) = delete;
     66   SourceLocExprScopeGuard &operator=(SourceLocExprScopeGuard const &) = delete;
     67 
     68   CurrentSourceLocExprScope &Current;
     69   CurrentSourceLocExprScope OldVal;
     70   bool Enable;
     71 };
     72 
     73 } // end namespace clang
     74 
     75 #endif // LLVM_CLANG_AST_CURRENT_SOURCE_LOC_EXPR_SCOPE_H
     76