From: Rafael Espindola Date: Thu, 11 Apr 2013 14:06:34 +0000 (+0000) Subject: Add a function to check if an argument list is too long. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=845a932af74ecbd2a20af5751dd61fa8cf2246f5;p=oota-llvm.git Add a function to check if an argument list is too long. This will be used in clang to decide if it should create an @file or not. It will be tested on the clang side. Patch by Nathan Froyd. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179285 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Program.h b/include/llvm/Support/Program.h index bf650112f28..fb177de97b4 100644 --- a/include/llvm/Support/Program.h +++ b/include/llvm/Support/Program.h @@ -14,6 +14,7 @@ #ifndef LLVM_SUPPORT_PROGRAM_H #define LLVM_SUPPORT_PROGRAM_H +#include "llvm/ADT/ArrayRef.h" #include "llvm/Support/Path.h" namespace llvm { @@ -140,6 +141,10 @@ namespace sys { /// @} }; + + // Return true if the given arguments fit within system-specific + // argument length limits. + bool argumentsFitWithinSystemLimits(ArrayRef Args); } } diff --git a/lib/Support/Unix/Program.inc b/lib/Support/Unix/Program.inc index 117151c91d8..aa03d48438e 100644 --- a/lib/Support/Unix/Program.inc +++ b/lib/Support/Unix/Program.inc @@ -32,6 +32,9 @@ #if HAVE_FCNTL_H #include #endif +#if HAVE_UNISTD_H +#include +#endif #ifdef HAVE_POSIX_SPAWN #include #if !defined(__APPLE__) @@ -409,4 +412,25 @@ error_code Program::ChangeStderrToBinary(){ return make_error_code(errc::success); } +bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef Args) { + static long ArgMax = sysconf(_SC_ARG_MAX); + + // System says no practical limit. + if (ArgMax == -1) + return true; + + // Conservatively account for space required by environment variables. + ArgMax /= 2; + + size_t ArgLength = 0; + for (ArrayRef::iterator I = Args.begin(), E = Args.end(); + I != E; ++I) { + ArgLength += strlen(*I) + 1; + if (ArgLength > size_t(ArgMax)) { + return false; + } + } + return true; +} + } diff --git a/lib/Support/Windows/Program.inc b/lib/Support/Windows/Program.inc index 691d6d45550..994a09764e3 100644 --- a/lib/Support/Windows/Program.inc +++ b/lib/Support/Windows/Program.inc @@ -396,4 +396,20 @@ error_code Program::ChangeStderrToBinary(){ return make_error_code(errc::success); } +bool llvm::sys::argumentsFitWithinSystemLimits(ArrayRef Args) { + // The documented max length of the command line passed to CreateProcess. + static const size_t MaxCommandStringLength = 32768; + size_t ArgLength = 0; + for (ArrayRef::iterator I = Args.begin(), E = Args.end(); + I != E; ++I) { + // Account for the trailing space for every arg but the last one and the + // trailing NULL of the last argument. + ArgLength += ArgLenWithQuotes(*I) + 1; + if (ArgLength > MaxCommandStringLength) { + return false; + } + } + return true; +} + }