CurrentSourceLocExprScope.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- CurrentSourceLocExprScope.h ----------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines types used to track the current context needed to evaluate
  15. // a SourceLocExpr.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_CURRENTSOURCELOCEXPRSCOPE_H
  19. #define LLVM_CLANG_AST_CURRENTSOURCELOCEXPRSCOPE_H
  20. #include <cassert>
  21. namespace clang {
  22. class Expr;
  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. public:
  30. /// A RAII style scope guard used for tracking the current source
  31. /// location and context as used by the source location builtins
  32. /// (ex. __builtin_LINE).
  33. class SourceLocExprScopeGuard;
  34. const Expr *getDefaultExpr() const { return DefaultExpr; }
  35. explicit CurrentSourceLocExprScope() = default;
  36. private:
  37. explicit CurrentSourceLocExprScope(const Expr *DefaultExpr)
  38. : DefaultExpr(DefaultExpr) {}
  39. CurrentSourceLocExprScope(CurrentSourceLocExprScope const &) = default;
  40. CurrentSourceLocExprScope &
  41. operator=(CurrentSourceLocExprScope const &) = default;
  42. };
  43. class CurrentSourceLocExprScope::SourceLocExprScopeGuard {
  44. public:
  45. SourceLocExprScopeGuard(const Expr *DefaultExpr,
  46. CurrentSourceLocExprScope &Current)
  47. : Current(Current), OldVal(Current), Enable(false) {
  48. assert(DefaultExpr && "the new scope should not be empty");
  49. if ((Enable = (Current.getDefaultExpr() == nullptr)))
  50. Current = CurrentSourceLocExprScope(DefaultExpr);
  51. }
  52. ~SourceLocExprScopeGuard() {
  53. if (Enable)
  54. Current = OldVal;
  55. }
  56. private:
  57. SourceLocExprScopeGuard(SourceLocExprScopeGuard const &) = delete;
  58. SourceLocExprScopeGuard &operator=(SourceLocExprScopeGuard const &) = delete;
  59. CurrentSourceLocExprScope &Current;
  60. CurrentSourceLocExprScope OldVal;
  61. bool Enable;
  62. };
  63. } // end namespace clang
  64. #endif // LLVM_CLANG_AST_CURRENTSOURCELOCEXPRSCOPE_H
  65. #ifdef __GNUC__
  66. #pragma GCC diagnostic pop
  67. #endif