macros.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. namespace Carbon::Check {
  10. auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
  11. SemIR::NameId name_id,
  12. clang::MacroInfo* macro_info) -> clang::Expr* {
  13. auto name_str_opt = context.names().GetAsStringIfIdentifier(name_id);
  14. CARBON_CHECK(macro_info, "macro info missing");
  15. if (macro_info->getNumTokens() == 0) {
  16. context.TODO(loc_id, "Unsupported: macro with 0 replacement tokens");
  17. return nullptr;
  18. }
  19. clang::Sema& sema = context.clang_sema();
  20. clang::Preprocessor& preprocessor = sema.getPreprocessor();
  21. clang::Parser parser(preprocessor, sema, false);
  22. llvm::SmallVector<clang::Token> tokens(macro_info->tokens().begin(),
  23. macro_info->tokens().end());
  24. clang::Token current_token = parser.getCurToken();
  25. // Add eof token
  26. clang::Token eof;
  27. eof.startToken();
  28. eof.setKind(clang::tok::eof);
  29. eof.setLocation(current_token.getEndLoc());
  30. tokens.push_back(eof);
  31. tokens.push_back(current_token);
  32. preprocessor.EnterTokenStream(tokens, false, false);
  33. parser.ConsumeAnyToken(true);
  34. clang::ExprResult result = parser.ParseConstantExpression();
  35. clang::Expr* result_expr = result.get();
  36. bool success =
  37. !result.isInvalid() && parser.getCurToken().is(clang::tok::eof);
  38. if (!success) {
  39. parser.SkipUntil(clang::tok::eof);
  40. CARBON_DIAGNOSTIC(
  41. InCppMacroEvaluation, Error,
  42. "failed to evaluate macro Cpp.{0} to a valid constant expression",
  43. std::string);
  44. context.emitter().Emit(loc_id, InCppMacroEvaluation, (*name_str_opt).str());
  45. return nullptr;
  46. }
  47. clang::Expr::EvalResult evaluated_result;
  48. if (!result_expr->EvaluateAsInt(evaluated_result, sema.getASTContext())) {
  49. context.TODO(loc_id, "non-integer constant expression in macro.");
  50. return nullptr;
  51. }
  52. return clang::IntegerLiteral::Create(
  53. sema.getASTContext(), evaluated_result.Val.getInt(),
  54. result_expr->getType(), result_expr->getExprLoc());
  55. }
  56. } // namespace Carbon::Check