[llvm-commits] [llvm] r79890 - /llvm/trunk/lib/Support/raw_ostream.cpp

Chris Lattner clattner at apple.com
Sun Aug 23 21:29:34 PDT 2009


On Aug 23, 2009, at 9:13 PM, Dan Gohman wrote:

> Author: djg
> Date: Sun Aug 23 23:13:01 2009
> New Revision: 79890
>
> URL: http://llvm.org/viewvc/llvm-project?rev=79890&view=rev
> Log:
> raw_ostream::indent is used for PadToColumn which often prints more
> than 16 spaces. Make the Spaces array wide enough to handle common  
> cases.

This will fail in the corner case when the indentation is exactly the  
length of the string (because of the nul terminator).  Please change:

> +  if (NumSpaces <= array_lengthof(Spaces))

to:

> +  if (NumSpaces < array_lengthof(Spaces))


and add a -1 to the other array_lengthof.

-Chris

>
> Modified:
>    llvm/trunk/lib/Support/raw_ostream.cpp
>
> Modified: llvm/trunk/lib/Support/raw_ostream.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/raw_ostream.cpp?rev=79890&r1=79889&r2=79890&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- llvm/trunk/lib/Support/raw_ostream.cpp (original)
> +++ llvm/trunk/lib/Support/raw_ostream.cpp Sun Aug 23 23:13:01 2009
> @@ -19,6 +19,7 @@
> #include "llvm/Config/config.h"
> #include "llvm/Support/Compiler.h"
> #include "llvm/Support/ErrorHandling.h"
> +#include "llvm/ADT/STLExtras.h"
> #include "llvm/ADT/StringExtras.h"
> #include <sys/stat.h>
> #include <sys/types.h>
> @@ -298,14 +299,16 @@
>
> /// indent - Insert 'NumSpaces' spaces.
> raw_ostream &raw_ostream::indent(unsigned NumSpaces) {
> -  const char *Spaces = "                ";
> +  static const char Spaces[] = "                                "
> +                               "                                "
> +                               "                ";
>
>   // Usually the indentation is small, handle it with a fastpath.
> -  if (NumSpaces <= 16)
> +  if (NumSpaces <= array_lengthof(Spaces))
>     return write(Spaces, NumSpaces);
>
>   while (NumSpaces) {
> -    unsigned NumToWrite = std::min(NumSpaces, 16U);
> +    unsigned NumToWrite = std::min(NumSpaces, (unsigned) 
> array_lengthof(Spaces));
>     write(Spaces, NumToWrite);
>     NumSpaces -= NumToWrite;
>   }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits




More information about the llvm-commits mailing list