fuzzer_util.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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 "executable_semantics/fuzzing/fuzzer_util.h"
  5. #include "common/check.h"
  6. #include "common/fuzzing/proto_to_carbon.h"
  7. namespace Carbon {
  8. // Appended to fuzzer-generated Carbon source when the source is missing
  9. // `Main()` definition, to prevent early error return in semantic analysis.
  10. static constexpr char EmptyMain[] = R"(
  11. fn Main() -> i32 {
  12. return 0;
  13. }
  14. )";
  15. auto ProtoToCarbonWithMain(const Fuzzing::CompilationUnit& compilation_unit)
  16. -> std::string {
  17. const bool has_main = std::any_of(
  18. compilation_unit.declarations().begin(),
  19. compilation_unit.declarations().end(),
  20. [](const Fuzzing::Declaration& decl) {
  21. return decl.kind_case() == Fuzzing::Declaration::kFunction &&
  22. decl.function().name() == "Main";
  23. });
  24. return Carbon::ProtoToCarbon(compilation_unit) + (has_main ? "" : EmptyMain);
  25. }
  26. } // namespace Carbon