macros.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // TODO: Identifiers are still only available if prefixed with "::" (e.g.
  37. // "#define M_Var ::myVar").
  38. parser.EnterScope(clang::Scope::DeclScope);
  39. clang::ExprResult result = parser.ParseConstantExpression();
  40. parser.ExitScope();
  41. clang::Expr* result_expr = result.get();
  42. bool success =
  43. !result.isInvalid() && parser.getCurToken().is(clang::tok::eof);
  44. if (!success) {
  45. parser.SkipUntil(clang::tok::eof);
  46. CARBON_DIAGNOSTIC(
  47. InCppMacroEvaluation, Error,
  48. "failed to parse macro Cpp.{0} to a valid constant expression",
  49. std::string);
  50. context.emitter().Emit(loc_id, InCppMacroEvaluation, (*name_str_opt).str());
  51. return nullptr;
  52. }
  53. result_expr = result_expr->IgnoreParenImpCasts();
  54. if (isa<clang::StringLiteral>(result_expr) ||
  55. isa<clang::CharacterLiteral>(result_expr) ||
  56. isa<clang::CXXNullPtrLiteralExpr>(result_expr)) {
  57. return result_expr;
  58. }
  59. clang::Expr::EvalResult evaluated_result;
  60. CARBON_CHECK(result_expr->EvaluateAsConstantExpr(evaluated_result,
  61. sema.getASTContext()));
  62. clang::APValue ap_value = evaluated_result.Val;
  63. // TODO: Add support for other types.
  64. if (ap_value.isLValue()) {
  65. if (!result_expr->EvaluateAsInt(evaluated_result, sema.getASTContext())) {
  66. context.TODO(loc_id,
  67. "Unsupported: macro evaluated to a non-integer LValue");
  68. return nullptr;
  69. }
  70. ap_value = evaluated_result.Val;
  71. }
  72. switch (ap_value.getKind()) {
  73. case clang::APValue::Int:
  74. if (result_expr->getType()->isBooleanType()) {
  75. return clang::CXXBoolLiteralExpr::Create(
  76. sema.getASTContext(), ap_value.getInt().getBoolValue(),
  77. result_expr->getType(), result_expr->getExprLoc());
  78. }
  79. return clang::IntegerLiteral::Create(
  80. sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
  81. result_expr->getExprLoc());
  82. case clang::APValue::Float:
  83. return clang::FloatingLiteral::Create(
  84. sema.getASTContext(), ap_value.getFloat(),
  85. /*isExact=*/true, result_expr->getType(), result_expr->getExprLoc());
  86. default:
  87. context.TODO(loc_id,
  88. "Unsupported: macro evaluated to a constant of type: " +
  89. result_expr->getType().getAsString());
  90. return nullptr;
  91. }
  92. }
  93. } // namespace Carbon::Check