[PATCH] D25929: Add llvm-echo command.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 24 17:39:24 PDT 2016


ruiu created this revision.
ruiu added reviewers: rnk, inglorion.
ruiu added a subscriber: llvm-commits.
Herald added subscribers: modocache, mgorny, beanz.

"echo" command is not very portable. Particularly on Windows, we have
various types of echo command that interpret backslashes, double-quotes
and single-quotes differently. On windows, shell does not tokenize
command line arguments but each command does, so the interpretation
varies depending on your crt.

As a result, we observed hard-to-fix errors that happened only on a
limited set of Windows buildbots.

This patch adds a portable "echo" command which always interprets
arguments in the Unix style even on Windows. llvm-echo returns the
same string for a string no matter what platform it is running.
This command should fix the compatibility issue.


https://reviews.llvm.org/D25929

Files:
  CMakeLists.txt
  utils/llvm-echo/CMakeLists.txt
  utils/llvm-echo/llvm-echo.cpp


Index: utils/llvm-echo/llvm-echo.cpp
===================================================================
--- /dev/null
+++ utils/llvm-echo/llvm-echo.cpp
@@ -0,0 +1,43 @@
+//===- llvm-echo.cpp - The 'echo' command ---------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is an implementation of a portable "echo" command.
+// It tokenizes command line arguments in the Unix style even on Windows.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/StringSaver.h"
+#include <iostream>
+
+#if LLVM_ON_WIN32
+#include <windows.h>
+#endif
+
+using namespace llvm;
+
+int main(int Argc, const char **Argv) {
+  SmallVector<const char *, 4> Args;
+
+#if LLVM_ON_WIN32
+  const char *Cmdline = GetCommandLineA();
+  BumpPtrAllocator Alloc;
+  StringSaver Saver(Alloc);
+  llvm::cl::TokenizeGNUCommandLine(Cmdline, Saver, Args);
+#else
+  Args.insert(Args.begin(), Argv, Argv + Argc);
+#endif
+
+  for (int I = 1, E = Args.size(); I < E; ++I) {
+    std::cout << Args[I];
+    std::cout << (I == E - 1 ? "\n" : " ");
+  }
+  return 0;
+}
Index: utils/llvm-echo/CMakeLists.txt
===================================================================
--- /dev/null
+++ utils/llvm-echo/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_llvm_utility(llvm-echo llvm-echo.cpp)
+
+target_link_libraries(llvm-echo LLVMSupport)
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -782,6 +782,7 @@
   add_subdirectory(utils/PerfectShuffle)
   add_subdirectory(utils/count)
   add_subdirectory(utils/not)
+  add_subdirectory(utils/llvm-echo)
   add_subdirectory(utils/llvm-lit)
   add_subdirectory(utils/yaml-bench)
   add_subdirectory(utils/unittest)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25929.75663.patch
Type: text/x-patch
Size: 2089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161025/7af6c1fd/attachment.bin>


More information about the llvm-commits mailing list