[llvm] r185716 - Add a higher level createTemporaryFile function.

Hal Finkel hfinkel at anl.gov
Thu Jul 25 07:01:00 PDT 2013


----- Original Message -----
> Author: rafael
> Date: Fri Jul  5 14:56:49 2013
> New Revision: 185716
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=185716&view=rev
> Log:
> Add a higher level createTemporaryFile function.
> 
> This function is inspired by clang's Driver::GetTemporaryPath. It
> hides the
> pattern used for uniquing and requires simple file names that are
> always
> placed in the system temporary directory.
> 
> Modified:
>     llvm/trunk/include/llvm/Support/FileSystem.h
>     llvm/trunk/lib/Support/Path.cpp
> 
> Modified: llvm/trunk/include/llvm/Support/FileSystem.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=185716&r1=185715&r2=185716&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Support/FileSystem.h (original)
> +++ llvm/trunk/include/llvm/Support/FileSystem.h Fri Jul  5 14:56:49
> 2013
> @@ -575,6 +575,22 @@ error_code unique_file(const Twine &mode
>  error_code unique_file(const Twine &Model, SmallVectorImpl<char>
>  &ResultPath,
>                         bool MakeAbsolute = true);
>  
> +/// @brief Create a file in the system temporary directory.
> +///
> +/// The filename is of the form prefix-random_chars.suffix. Since
> the directory
> +/// is not know to the caller, Prefix and Suffix cannot have path
> separators.
> +/// The files are created with mode 0600.
> +///
> +/// This should be used for things like a temporary .s that is
> removed after
> +/// running the assembler.
> +error_code createTemporaryFile(const Twine &Prefix, StringRef
> Suffix,
> +                               int &ResultFD,
> +                               SmallVectorImpl<char> &ResultPath);
> +
> +/// @brief Simpler version for clients that don't want an open file.
> +error_code createTemporaryFile(const Twine &Prefix, StringRef
> Suffix,
> +                               SmallVectorImpl<char> &ResultPath);
> +
>  error_code createUniqueDirectory(const Twine &Prefix,
>                                   SmallVectorImpl<char> &ResultPath);
>  
> 
> Modified: llvm/trunk/lib/Support/Path.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=185716&r1=185715&r2=185716&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/Path.cpp (original)
> +++ llvm/trunk/lib/Support/Path.cpp Fri Jul  5 14:56:49 2013
> @@ -654,6 +654,40 @@ error_code unique_file(const Twine &Mode
>    return createUniqueEntity(Model, Dummy, ResultPath, MakeAbsolute,
>    0, FS_Name);
>  }
>  
> +static error_code createTemporaryFile(const Twine &Model, int
> &ResultFD,
> +                                      llvm::SmallVectorImpl<char>
> &ResultPath,
> +                                      FSEntity Type) {
> +  SmallString<128> Storage;
> +  StringRef P = Model.toNullTerminatedStringRef(Storage);
> +  assert(P.find_first_of(separators) == StringRef::npos &&
> +         "Model must be a simple filename.");
> +  // Use P.begin() so that createUniqueEntity doesn't need to
> recreate Storage.
> +  return createUniqueEntity(P.begin(), ResultFD, ResultPath,
> +                            true, owner_read | owner_write, Type);
> +}
> +
> +static error_code
> +createTemporaryFile(const Twine &Prefix, StringRef Suffix, int
> &ResultFD,
> +                    llvm::SmallVectorImpl<char> &ResultPath,
> +                    FSEntity Type) {
> +  return createTemporaryFile(Prefix + "-%%%%%%." + Suffix, ResultFD,
> ResultPath,
> +                             Type);

When Suffix == "", this creates a file with a trailing '.', and while there is nothing functionally wrong with this, it does look a bit odd. Do you think that we could make this:

Prefix + "-%%%%%%" + (Suffix.empty() ? StringRef("") : StringRef(".")) + Suffix

Or something similar instead?

Thanks again,
Hal


> +}
> +
> +
> +error_code createTemporaryFile(const Twine &Prefix, StringRef
> Suffix,
> +                               int &ResultFD,
> +                               SmallVectorImpl<char> &ResultPath) {
> +  return createTemporaryFile(Prefix, Suffix, ResultFD, ResultPath,
> FS_File);
> +}
> +
> +error_code createTemporaryFile(const Twine &Prefix, StringRef
> Suffix,
> +                               SmallVectorImpl<char> &ResultPath) {
> +  int Dummy;
> +  return createTemporaryFile(Prefix, Suffix, Dummy, ResultPath,
> FS_Name);
> +}
> +
> +
>  // This is a mkdtemp with a different pattern. We use
>  createUniqueEntity mostly
>  // for consistency. We should try using mkdtemp.
>  error_code createUniqueDirectory(const Twine &Prefix,
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
> 

-- 
Hal Finkel
Assistant Computational Scientist
Leadership Computing Facility
Argonne National Laboratory



More information about the llvm-commits mailing list