macros.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp/macros.h"
  5. #include "clang/AST/ASTContext.h"
  6. #include "clang/AST/Expr.h"
  7. #include "clang/Parse/Parser.h"
  8. #include "clang/Sema/Sema.h"
  9. #include "common/check.h"
  10. namespace Carbon::Check {
  11. auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
  12. SemIR::NameId name_id,
  13. clang::MacroInfo* macro_info) -> clang::Expr* {
  14. auto name_str_opt = context.names().GetAsStringIfIdentifier(name_id);
  15. CARBON_CHECK(macro_info, "macro info missing");
  16. if (macro_info->getNumTokens() == 0) {
  17. context.TODO(loc_id, "Unsupported: macro with 0 replacement tokens");
  18. return nullptr;
  19. }
  20. clang::Sema& sema = context.clang_sema();
  21. clang::Preprocessor& preprocessor = sema.getPreprocessor();
  22. auto& parser = context.cpp_context()->parser();
  23. llvm::SmallVector<clang::Token> tokens(macro_info->tokens().begin(),
  24. macro_info->tokens().end());
  25. clang::Token current_token = parser.getCurToken();
  26. // Add eof token
  27. clang::Token eof;
  28. eof.startToken();
  29. eof.setKind(clang::tok::eof);
  30. eof.setLocation(current_token.getEndLoc());
  31. tokens.push_back(eof);
  32. tokens.push_back(current_token);
  33. preprocessor.EnterTokenStream(tokens, /*DisableMacroExpansion=*/false,
  34. /*IsReinject=*/false);
  35. parser.ConsumeAnyToken(true);
  36. clang::ExprResult result = parser.ParseConstantExpression();
  37. clang::Expr* result_expr = result.get();
  38. bool success =
  39. !result.isInvalid() && parser.getCurToken().is(clang::tok::eof);
  40. if (!success) {
  41. parser.SkipUntil(clang::tok::eof);
  42. CARBON_DIAGNOSTIC(
  43. InCppMacroEvaluation, Error,
  44. "failed to parse macro Cpp.{0} to a valid constant expression",
  45. std::string);
  46. context.emitter().Emit(loc_id, InCppMacroEvaluation, (*name_str_opt).str());
  47. return nullptr;
  48. }
  49. result_expr = result_expr->IgnoreParenImpCasts();
  50. if (isa<clang::StringLiteral>(result_expr) ||
  51. isa<clang::CharacterLiteral>(result_expr) ||
  52. isa<clang::CXXNullPtrLiteralExpr>(result_expr)) {
  53. return result_expr;
  54. }
  55. clang::Expr::EvalResult evaluated_result;
  56. CARBON_CHECK(result_expr->EvaluateAsConstantExpr(evaluated_result,
  57. sema.getASTContext()));
  58. clang::APValue ap_value = evaluated_result.Val;
  59. // TODO: Add support for other types.
  60. if (ap_value.isLValue()) {
  61. if (!result_expr->EvaluateAsInt(evaluated_result, sema.getASTContext())) {
  62. context.TODO(loc_id,
  63. "Unsupported: macro evaluated to a non-integer LValue");
  64. return nullptr;
  65. }
  66. ap_value = evaluated_result.Val;
  67. }
  68. switch (ap_value.getKind()) {
  69. case clang::APValue::Int:
  70. if (result_expr->getType()->isBooleanType()) {
  71. return clang::CXXBoolLiteralExpr::Create(
  72. sema.getASTContext(), ap_value.getInt().getBoolValue(),
  73. result_expr->getType(), result_expr->getExprLoc());
  74. }
  75. return clang::IntegerLiteral::Create(
  76. sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
  77. result_expr->getExprLoc());
  78. case clang::APValue::Float:
  79. return clang::FloatingLiteral::Create(
  80. sema.getASTContext(), ap_value.getFloat(),
  81. /*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
  82. default:
  83. context.TODO(loc_id,
  84. "Unsupported: macro evaluated to a constant of type: " +
  85. result_expr->getType().getAsString());
  86. return nullptr;
  87. }
  88. }
  89. } // namespace Carbon::Check