[libc][WrapperGen] Replace the C _Noreturn annotation with C++ [[noreturn]].
authorSiva Chandra Reddy <sivachandra@google.com>
Fri, 6 Nov 2020 01:07:17 +0000 (17:07 -0800)
committerSiva Chandra Reddy <sivachandra@google.com>
Fri, 6 Nov 2020 19:37:48 +0000 (11:37 -0800)
Reviewed By: michaelrj

Differential Revision: https://reviews.llvm.org/D90900

libc/utils/tools/WrapperGen/Main.cpp

index 0b064bc..3745759 100644 (file)
@@ -38,8 +38,18 @@ static bool WrapperGenMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
   llvm::Record *FunctionSpec = NameSpecPair.second;
   llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
   llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");
-  OS << "extern \"C\" " << Indexer.getTypeAsString(ReturnType) << " "
-     << FunctionName << "(";
+  std::string ReturnTypeString = Indexer.getTypeAsString(ReturnType);
+  bool ShouldReturn = true;
+  // We are generating C wrappers in C++ code. So, we should convert the C
+  // _Noreturn to the C++ [[noreturn]].
+  llvm::StringRef NR("_Noreturn "); // Note the space after _Noreturn
+  llvm::StringRef RT(ReturnTypeString);
+  if (RT.startswith(NR)) {
+    RT = RT.drop_front(NR.size() - 1); // - 1 because of the space.
+    ReturnTypeString = std::string("[[noreturn]]") + std::string(RT);
+    ShouldReturn = false;
+  }
+  OS << "extern \"C\" " << ReturnTypeString << " " << FunctionName << "(";
 
   auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
   std::stringstream CallArgs;
@@ -73,8 +83,8 @@ static bool WrapperGenMain(llvm::raw_ostream &OS, llvm::RecordKeeper &Records) {
   // match the standard types. Either handle such differences here, or
   // avoid such a thing in the implementations.
   OS << ") {\n"
-     << "  return __llvm_libc::" << FunctionName << "(" << CallArgs.str()
-     << ");\n"
+     << "  " << (ShouldReturn ? "return " : "")
+     << "__llvm_libc::" << FunctionName << "(" << CallArgs.str() << ");\n"
      << "}\n";
 
   return false;