macros.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. clang::Parser parser(preprocessor, sema, false);
  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. if (isa<clang::StringLiteral>(result_expr) ||
  50. isa<clang::CharacterLiteral>(result_expr)) {
  51. return result_expr;
  52. }
  53. clang::Expr::EvalResult evaluated_result;
  54. CARBON_CHECK(result_expr->EvaluateAsConstantExpr(evaluated_result,
  55. sema.getASTContext()));
  56. clang::APValue ap_value = evaluated_result.Val;
  57. switch (ap_value.getKind()) {
  58. case clang::APValue::Int:
  59. if (result_expr->getType()->isBooleanType()) {
  60. return clang::CXXBoolLiteralExpr::Create(
  61. sema.getASTContext(), ap_value.getInt().getBoolValue(),
  62. result_expr->getType(), result_expr->getExprLoc());
  63. }
  64. return clang::IntegerLiteral::Create(
  65. sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
  66. result_expr->getExprLoc());
  67. case clang::APValue::Float:
  68. return clang::FloatingLiteral::Create(
  69. sema.getASTContext(), ap_value.getFloat(),
  70. /*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
  71. default:
  72. context.TODO(loc_id,
  73. "Unsupported: macro evaluated to a constant of type: " +
  74. result_expr->getType().getAsString());
  75. return nullptr;
  76. }
  77. }
  78. } // namespace Carbon::Check