[cfe-commits] r157445 - /cfe/trunk/lib/Frontend/TextDiagnostic.cpp

Seth Cantrell seth.cantrell at gmail.com
Thu May 24 18:34:31 PDT 2012


On May 24, 2012, at 8:50 PM, Marshall Clow wrote:

> On May 24, 2012, at 5:03 PM, Seth Cantrell wrote:
> 
>> Author: socantre
>> Date: Thu May 24 19:03:29 2012
>> New Revision: 157445
>> 
>> URL: http://llvm.org/viewvc/llvm-project?rev=157445&view=rev
>> Log:
>> ensure value passed to is space is representable as unsigned char
> 
> Note that in C++ you can use the current facet which has a templated isspace function, so you don't have to use the C 'isspace'. (with all its' limitations)
> 

It probably would be good to do that, and in particular to only use std::locale("C") in this case, since this code should not be locale sensitive.

Thanks,
Seth


> namespace std {
> 	template <class charT> bool isspace (charT c, const locale& loc);
> 	};
> 
> and you can get a copy of the current locale using std::locale ()
> 
> Example program:
> 
> #include <iostream>
> #include <locale>
> 
> int main(int argc, char *argv[] ) {
> 	const char *p = argv[1];
> 	std::locale loc = std::locale ();	// copy of current locale
>    while (*p) {
>    	if ( std::isspace (*p, loc))
>    		std::cout << '*';
>    	else
>    		std::cout << *p;
> 		p++;
> 		}    		
>   	 std::cout << std::endl;
> 
>    return 0;
> }
> 
> 
>> 
>> if the value isn't an unsigned char or EOF behavior is undefined
>> (and on Windows there's an assertion)
>> 
>> Modified:
>>   cfe/trunk/lib/Frontend/TextDiagnostic.cpp
>> 
>> Modified: cfe/trunk/lib/Frontend/TextDiagnostic.cpp
>> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnostic.cpp?rev=157445&r1=157444&r2=157445&view=diff
>> ==============================================================================
>> --- cfe/trunk/lib/Frontend/TextDiagnostic.cpp (original)
>> +++ cfe/trunk/lib/Frontend/TextDiagnostic.cpp Thu May 24 19:03:29 2012
>> @@ -307,11 +307,11 @@
>>  // correctly.
>>  unsigned CaretStart = 0, CaretEnd = CaretLine.size();
>>  for (; CaretStart != CaretEnd; ++CaretStart)
>> -    if (!isspace(CaretLine[CaretStart]))
>> +    if (!isspace(static_cast<unsigned char>(CaretLine[CaretStart])))
>>      break;
>> 
>>  for (; CaretEnd != CaretStart; --CaretEnd)
>> -    if (!isspace(CaretLine[CaretEnd - 1]))
>> +    if (!isspace(static_cast<unsigned char>(CaretLine[CaretEnd - 1])))
>>      break;
>> 
>>  // caret has already been inserted into CaretLine so the above whitespace
>> @@ -322,11 +322,11 @@
>>  if (!FixItInsertionLine.empty()) {
>>    unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
>>    for (; FixItStart != FixItEnd; ++FixItStart)
>> -      if (!isspace(FixItInsertionLine[FixItStart]))
>> +      if (!isspace(static_cast<unsigned char>(FixItInsertionLine[FixItStart])))
>>        break;
>> 
>>    for (; FixItEnd != FixItStart; --FixItEnd)
>> -      if (!isspace(FixItInsertionLine[FixItEnd - 1]))
>> +      if (!isspace(static_cast<unsigned char>(FixItInsertionLine[FixItEnd - 1])))
>>        break;
>> 
>>    CaretStart = std::min(FixItStart, CaretStart);
>> @@ -382,12 +382,14 @@
>>      // Skip over any whitespace we see here; we're looking for
>>      // another bit of interesting text.
>>      while (NewStart &&
>> -             (map.byteToColumn(NewStart)==-1 || isspace(SourceLine[NewStart])))
>> +             (map.byteToColumn(NewStart)==-1 ||
>> +             isspace(static_cast<unsigned char>(SourceLine[NewStart]))))
>>        --NewStart;
>> 
>>      // Skip over this bit of "interesting" text.
>>      while (NewStart &&
>> -             (map.byteToColumn(NewStart)!=-1 && !isspace(SourceLine[NewStart])))
>> +             (map.byteToColumn(NewStart)!=-1 &&
>> +             !isspace(static_cast<unsigned char>(SourceLine[NewStart]))))
>>        --NewStart;
>> 
>>      // Move up to the non-whitespace character we just saw.
>> @@ -408,12 +410,14 @@
>>      // Skip over any whitespace we see here; we're looking for
>>      // another bit of interesting text.
>>      while (NewEnd<SourceLine.size() &&
>> -             (map.byteToColumn(NewEnd)==-1 || isspace(SourceLine[NewEnd])))
>> +             (map.byteToColumn(NewEnd)==-1 ||
>> +             isspace(static_cast<unsigned char>(SourceLine[NewEnd]))))
>>        ++NewEnd;
>> 
>>      // Skip over this bit of "interesting" text.
>>      while (NewEnd<SourceLine.size() &&
>> -             (map.byteToColumn(NewEnd)!=-1 && !isspace(SourceLine[NewEnd])))
>> +             (map.byteToColumn(NewEnd)!=-1 &&
>> +             !isspace(static_cast<unsigned char>(SourceLine[NewEnd]))))
>>        ++NewEnd;
>> 
>>      unsigned NewColumns = map.byteToColumn(NewEnd) -
>> 
>> 
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> 
> -- Marshall
> 
> Marshall Clow     Idio Software   <mailto:mclow.lists at gmail.com>
> 
> A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
>        -- Yu Suzuki
> 





More information about the cfe-commits mailing list