Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 10 | // This file implements the IdentifierInfo, IdentifierVisitor, and |
Chris Lattner | 91cbf11 | 2006-07-03 04:28:52 +0000 | [diff] [blame] | 11 | // IdentifierTable interfaces. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/IdentifierTable.h" |
| 16 | #include "clang/Lex/MacroInfo.h" |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 18 | #include <iostream> |
| 19 | using namespace llvm; |
| 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 23 | // IdentifierInfo Implementation |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 26 | void IdentifierInfo::Destroy() { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 27 | delete Macro; |
| 28 | } |
| 29 | |
Chris Lattner | 91cbf11 | 2006-07-03 04:28:52 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // IdentifierVisitor Implementation |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | IdentifierVisitor::~IdentifierVisitor() { |
| 35 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Memory Allocation Support |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | /// The identifier table has a very simple memory allocation pattern: it just |
| 42 | /// keeps allocating identifiers, then never frees them unless it frees them |
| 43 | /// all. As such, we use a simple bump-pointer memory allocator to make |
| 44 | /// allocation speedy. Shark showed that malloc was 27% of the time spent in |
| 45 | /// IdentifierTable::getIdentifier with malloc, and takes a 4.3% time with this. |
| 46 | #define USE_ALLOCATOR 1 |
| 47 | #if USE_ALLOCATOR |
| 48 | |
| 49 | namespace { |
| 50 | class MemRegion { |
| 51 | unsigned RegionSize; |
| 52 | MemRegion *Next; |
| 53 | char *NextPtr; |
| 54 | public: |
| 55 | void Init(unsigned size, MemRegion *next) { |
| 56 | RegionSize = size; |
| 57 | Next = next; |
| 58 | NextPtr = (char*)(this+1); |
| 59 | |
| 60 | // FIXME: uses GCC extension. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 61 | unsigned Alignment = __alignof__(IdentifierInfo); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 62 | NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) & |
| 63 | ~(intptr_t)(Alignment-1)); |
| 64 | } |
| 65 | |
| 66 | const MemRegion *getNext() const { return Next; } |
| 67 | unsigned getNumBytesAllocated() const { |
| 68 | return NextPtr-(const char*)this; |
| 69 | } |
| 70 | |
| 71 | /// Allocate - Allocate and return at least the specified number of bytes. |
| 72 | /// |
| 73 | void *Allocate(unsigned AllocSize, MemRegion **RegPtr) { |
| 74 | // FIXME: uses GCC extension. |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 75 | unsigned Alignment = __alignof__(IdentifierInfo); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 76 | // Round size up to an even multiple of the alignment. |
| 77 | AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1); |
| 78 | |
| 79 | // If there is space in this region for the identifier, return it. |
| 80 | if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) { |
| 81 | void *Result = NextPtr; |
| 82 | NextPtr += AllocSize; |
| 83 | return Result; |
| 84 | } |
| 85 | |
| 86 | // Otherwise, we have to allocate a new chunk. Create one twice as big as |
| 87 | // this one. |
| 88 | MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2); |
| 89 | NewRegion->Init(RegionSize*2, this); |
| 90 | |
| 91 | // Update the current "first region" pointer to point to the new region. |
| 92 | *RegPtr = NewRegion; |
| 93 | |
| 94 | // Try allocating from it now. |
| 95 | return NewRegion->Allocate(AllocSize, RegPtr); |
| 96 | } |
| 97 | |
| 98 | /// Deallocate - Release all memory for this region to the system. |
| 99 | /// |
| 100 | void Deallocate() { |
| 101 | MemRegion *next = Next; |
| 102 | free(this); |
| 103 | if (next) |
| 104 | next->Deallocate(); |
| 105 | } |
| 106 | }; |
| 107 | } |
| 108 | |
| 109 | #endif |
| 110 | |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | // IdentifierTable Implementation |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | |
| 115 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 116 | /// IdentifierBucket - The hash table consists of an array of these. If Info is |
| 117 | /// non-null, this is an extant entry, otherwise, it is a hole. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 118 | struct IdentifierBucket { |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 119 | /// FullHashValue - This remembers the full hash value of the identifier for |
| 120 | /// easy scanning. |
| 121 | unsigned FullHashValue; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 123 | /// Info - This is a pointer to the actual identifier info object. |
| 124 | IdentifierInfo *Info; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 127 | IdentifierTable::IdentifierTable(const LangOptions &LangOpts) { |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 128 | HashTableSize = 8192; // Start with space for 8K identifiers. |
| 129 | IdentifierBucket *TableArray = new IdentifierBucket[HashTableSize](); |
| 130 | memset(TableArray, 0, HashTableSize*sizeof(IdentifierBucket)); |
| 131 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 132 | TheTable = TableArray; |
| 133 | NumIdentifiers = 0; |
| 134 | #if USE_ALLOCATOR |
| 135 | TheMemory = malloc(8*4096); |
| 136 | ((MemRegion*)TheMemory)->Init(8*4096, 0); |
| 137 | #endif |
| 138 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 139 | // Populate the identifier table with info about keywords for the current |
| 140 | // language. |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 141 | AddKeywords(LangOpts); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | IdentifierTable::~IdentifierTable() { |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 145 | IdentifierBucket *TableArray = (IdentifierBucket*)TheTable; |
| 146 | for (unsigned i = 0, e = HashTableSize; i != e; ++i) { |
| 147 | if (IdentifierInfo *Id = TableArray[i].Info) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 148 | // Free memory referenced by the identifier (e.g. macro info). |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 149 | Id->Destroy(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 151 | #if !USE_ALLOCATOR |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 152 | // Free the memory for the identifier itself. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 153 | free(Id); |
| 154 | #endif |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | #if USE_ALLOCATOR |
| 158 | ((MemRegion*)TheMemory)->Deallocate(); |
| 159 | #endif |
| 160 | delete [] TableArray; |
| 161 | } |
| 162 | |
| 163 | /// HashString - Compute a hash code for the specified string. |
| 164 | /// |
| 165 | static unsigned HashString(const char *Start, const char *End) { |
| 166 | unsigned int Result = 0; |
| 167 | // Perl hash function. |
| 168 | while (Start != End) |
| 169 | Result = Result * 33 + *Start++; |
| 170 | Result = Result + (Result >> 5); |
| 171 | return Result; |
| 172 | } |
| 173 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 174 | IdentifierInfo &IdentifierTable::get(const char *NameStart, |
Chris Lattner | 0e1cf1f | 2006-07-04 18:53:52 +0000 | [diff] [blame] | 175 | const char *NameEnd) { |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 176 | IdentifierBucket *TableArray = (IdentifierBucket*)TheTable; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 177 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 178 | unsigned HTSize = HashTableSize; |
| 179 | unsigned FullHashValue = HashString(NameStart, NameEnd); |
| 180 | unsigned BucketNo = FullHashValue & (HTSize-1); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 181 | unsigned Length = NameEnd-NameStart; |
| 182 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 183 | unsigned ProbeAmt = 1; |
| 184 | while (1) { |
| 185 | IdentifierBucket &Bucket = TableArray[BucketNo]; |
| 186 | IdentifierInfo *BucketII = Bucket.Info; |
| 187 | // If we found an empty bucket, this identifier isn't in the table yet. |
| 188 | if (BucketII == 0) break; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 189 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 190 | // If the full hash value matches, check deeply for a match. The common |
| 191 | // case here is that we are only looking at the buckets (for identifier info |
| 192 | // being non-null and for the full hash value) not at the identifiers. This |
| 193 | // is important for cache locality. |
| 194 | if (Bucket.FullHashValue == FullHashValue && |
| 195 | BucketII->getNameLength() == Length && |
| 196 | memcmp(BucketII->getName(), NameStart, Length) == 0) |
| 197 | // We found a match! |
| 198 | return *BucketII; |
| 199 | |
| 200 | // Okay, we didn't find the identifier. Probe to the next bucket. |
| 201 | BucketNo = (BucketNo+ProbeAmt) & (HashTableSize-1); |
| 202 | |
| 203 | // Use quadratic probing, it has fewer clumping artifacts than linear |
| 204 | // probing and has good cache behavior in the common case. |
| 205 | ++ProbeAmt; |
| 206 | } |
| 207 | |
| 208 | // Okay, the identifier doesn't already exist, and BucketNo is the bucket to |
| 209 | // fill in. Allocate a new identifier with space for the null-terminated |
| 210 | // string at the end. |
| 211 | unsigned AllocSize = sizeof(IdentifierInfo)+Length+1; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 212 | #if USE_ALLOCATOR |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 213 | IdentifierInfo *Identifier = (IdentifierInfo*) |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 214 | ((MemRegion*)TheMemory)->Allocate(AllocSize, (MemRegion**)&TheMemory); |
| 215 | #else |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 216 | IdentifierInfo *Identifier = (IdentifierInfo*)malloc(AllocSize); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 217 | #endif |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 218 | Identifier->NameLen = Length; |
| 219 | Identifier->Macro = 0; |
| 220 | Identifier->TokenID = tok::identifier; |
| 221 | Identifier->PPID = tok::pp_not_keyword; |
| 222 | Identifier->ObjCID = tok::objc_not_keyword; |
| 223 | Identifier->IsExtension = false; |
| 224 | Identifier->IsPoisoned = false; |
| 225 | Identifier->IsOtherTargetMacro = false; |
| 226 | Identifier->FETokenInfo = 0; |
| 227 | ++NumIdentifiers; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 228 | |
| 229 | // Copy the string information. |
| 230 | char *StrBuffer = (char*)(Identifier+1); |
| 231 | memcpy(StrBuffer, NameStart, Length); |
| 232 | StrBuffer[Length] = 0; // Null terminate string. |
| 233 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 234 | // Fill in the bucket for the hash table. |
| 235 | TableArray[BucketNo].Info = Identifier; |
| 236 | TableArray[BucketNo].FullHashValue = FullHashValue; |
| 237 | |
| 238 | // If the hash table is now more than 3/4 full, rehash into a larger table. |
| 239 | if (NumIdentifiers > HashTableSize*3/4) |
| 240 | RehashTable(); |
| 241 | |
| 242 | return *Identifier; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 245 | IdentifierInfo &IdentifierTable::get(const std::string &Name) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 246 | // Don't use c_str() here: no need to be null terminated. |
| 247 | const char *NameBytes = &Name[0]; |
| 248 | unsigned Size = Name.size(); |
| 249 | return get(NameBytes, NameBytes+Size); |
| 250 | } |
| 251 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 252 | void IdentifierTable::RehashTable() { |
| 253 | unsigned NewSize = HashTableSize*2; |
| 254 | IdentifierBucket *NewTableArray = new IdentifierBucket[NewSize](); |
| 255 | memset(NewTableArray, 0, NewSize*sizeof(IdentifierBucket)); |
| 256 | |
| 257 | // Rehash all the identifier into their new buckets. Luckily we already have |
| 258 | // the hash values available :). |
| 259 | IdentifierBucket *CurTable = (IdentifierBucket *)TheTable; |
| 260 | for (IdentifierBucket *IB = CurTable, *E = CurTable+HashTableSize; |
| 261 | IB != E; ++IB) { |
| 262 | if (IB->Info) { |
| 263 | // Fast case, bucket available. |
| 264 | unsigned FullHash = IB->FullHashValue; |
| 265 | unsigned NewBucket = FullHash & (NewSize-1); |
| 266 | if (NewTableArray[NewBucket].Info == 0) { |
| 267 | NewTableArray[FullHash & (NewSize-1)].Info = IB->Info; |
| 268 | NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash; |
| 269 | continue; |
| 270 | } |
| 271 | |
| 272 | unsigned ProbeSize = 1; |
| 273 | do { |
| 274 | NewBucket = (NewBucket + ProbeSize++) & (NewSize-1); |
| 275 | } while (NewTableArray[NewBucket].Info); |
| 276 | |
| 277 | // Finally found a slot. Fill it in. |
| 278 | NewTableArray[FullHash & (NewSize-1)].Info = IB->Info; |
| 279 | NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | delete[] CurTable; |
| 284 | |
| 285 | TheTable = NewTableArray; |
| 286 | HashTableSize = NewSize; |
| 287 | } |
| 288 | |
| 289 | |
Chris Lattner | 91cbf11 | 2006-07-03 04:28:52 +0000 | [diff] [blame] | 290 | /// VisitIdentifiers - This method walks through all of the identifiers, |
| 291 | /// invoking IV->VisitIdentifier for each of them. |
| 292 | void IdentifierTable::VisitIdentifiers(const IdentifierVisitor &IV) { |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 293 | IdentifierBucket *TableArray = (IdentifierBucket*)TheTable; |
| 294 | for (unsigned i = 0, e = HashTableSize; i != e; ++i) { |
| 295 | if (IdentifierInfo *Id = TableArray[i].Info) |
| 296 | IV.VisitIdentifier(*Id); |
Chris Lattner | 91cbf11 | 2006-07-03 04:28:52 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 300 | //===----------------------------------------------------------------------===// |
| 301 | // Language Keyword Implementation |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | |
| 304 | /// AddKeyword - This method is used to associate a token ID with specific |
| 305 | /// identifiers because they are language keywords. This causes the lexer to |
| 306 | /// automatically map matching identifiers to specialized token codes. |
| 307 | /// |
| 308 | /// The C90/C99/CPP flags are set to 0 if the token should be enabled in the |
| 309 | /// specified langauge, set to 1 if it is an extension in the specified |
| 310 | /// language, and set to 2 if disabled in the specified language. |
| 311 | static void AddKeyword(const std::string &Keyword, tok::TokenKind TokenCode, |
Chris Lattner | a4271e4 | 2006-10-20 06:13:26 +0000 | [diff] [blame] | 312 | int C90, int C99, int CXX, |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 313 | const LangOptions &LangOpts, IdentifierTable &Table) { |
Chris Lattner | a4271e4 | 2006-10-20 06:13:26 +0000 | [diff] [blame] | 314 | int Flags = LangOpts.CPlusPlus ? CXX : (LangOpts.C99 ? C99 : C90); |
Chris Lattner | 25e0d54 | 2006-10-18 06:07:05 +0000 | [diff] [blame] | 315 | |
| 316 | // Don't add this keyword if disabled in this language or if an extension |
| 317 | // and extensions are disabled. |
| 318 | if (Flags + LangOpts.NoExtensions >= 2) return; |
| 319 | |
| 320 | const char *Str = &Keyword[0]; |
| 321 | IdentifierInfo &Info = Table.get(Str, Str+Keyword.size()); |
| 322 | Info.setTokenID(TokenCode); |
| 323 | Info.setIsExtensionToken(Flags == 1); |
| 324 | } |
| 325 | |
| 326 | /// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or |
| 327 | /// "elif". |
| 328 | static void AddPPKeyword(tok::PPKeywordKind PPID, |
| 329 | const char *Name, unsigned NameLen, |
| 330 | IdentifierTable &Table) { |
| 331 | Table.get(Name, Name+NameLen).setPPKeywordID(PPID); |
| 332 | } |
| 333 | |
| 334 | /// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or |
| 335 | /// "property". |
| 336 | static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID, |
| 337 | const char *Name, unsigned NameLen, |
| 338 | IdentifierTable &Table) { |
| 339 | Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID); |
| 340 | } |
| 341 | |
| 342 | /// AddKeywords - Add all keywords to the symbol table. |
| 343 | /// |
| 344 | void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { |
| 345 | enum { |
| 346 | C90Shift = 0, |
| 347 | EXTC90 = 1 << C90Shift, |
| 348 | NOTC90 = 2 << C90Shift, |
| 349 | C99Shift = 2, |
| 350 | EXTC99 = 1 << C99Shift, |
| 351 | NOTC99 = 2 << C99Shift, |
| 352 | CPPShift = 4, |
| 353 | EXTCPP = 1 << CPPShift, |
| 354 | NOTCPP = 2 << CPPShift, |
| 355 | Mask = 3 |
| 356 | }; |
| 357 | |
| 358 | // Add keywords and tokens for the current language. |
| 359 | #define KEYWORD(NAME, FLAGS) \ |
| 360 | AddKeyword(#NAME, tok::kw_ ## NAME, \ |
| 361 | ((FLAGS) >> C90Shift) & Mask, \ |
| 362 | ((FLAGS) >> C99Shift) & Mask, \ |
| 363 | ((FLAGS) >> CPPShift) & Mask, LangOpts, *this); |
| 364 | #define ALIAS(NAME, TOK) \ |
| 365 | AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0, LangOpts, *this); |
| 366 | #define PPKEYWORD(NAME) \ |
| 367 | AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this); |
| 368 | #define OBJC1_AT_KEYWORD(NAME) \ |
| 369 | if (LangOpts.ObjC1) \ |
| 370 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 371 | #define OBJC2_AT_KEYWORD(NAME) \ |
| 372 | if (LangOpts.ObjC2) \ |
| 373 | AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this); |
| 374 | #include "clang/Basic/TokenKinds.def" |
| 375 | } |
| 376 | |
| 377 | |
| 378 | //===----------------------------------------------------------------------===// |
| 379 | // Stats Implementation |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 382 | /// PrintStats - Print statistics about how well the identifier table is doing |
| 383 | /// at hashing identifiers. |
| 384 | void IdentifierTable::PrintStats() const { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 385 | unsigned NumEmptyBuckets = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 386 | unsigned AverageIdentifierSize = 0; |
| 387 | unsigned MaxIdentifierLength = 0; |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 388 | unsigned NumProbed = 0; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 389 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 390 | IdentifierBucket *TableArray = (IdentifierBucket*)TheTable; |
| 391 | for (unsigned i = 0, e = HashTableSize; i != e; ++i) { |
| 392 | if (TableArray[i].Info == 0) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 393 | ++NumEmptyBuckets; |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 394 | continue; |
| 395 | } |
| 396 | IdentifierInfo *Id = TableArray[i].Info; |
| 397 | |
| 398 | AverageIdentifierSize += Id->getNameLength(); |
| 399 | if (MaxIdentifierLength < Id->getNameLength()) |
| 400 | MaxIdentifierLength = Id->getNameLength(); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 401 | |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 402 | // Count the number of times something was probed. |
| 403 | if ((TableArray[i].FullHashValue & (e-1)) != i) |
| 404 | ++NumProbed; |
| 405 | |
| 406 | // TODO: Figure out maximum times an identifier had to probe for -stats. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | std::cerr << "\n*** Identifier Table Stats:\n"; |
| 410 | std::cerr << "# Identifiers: " << NumIdentifiers << "\n"; |
| 411 | std::cerr << "# Empty Buckets: " << NumEmptyBuckets << "\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 412 | std::cerr << "Hash density (#identifiers per bucket): " |
Chris Lattner | f2e3ac3 | 2006-10-27 03:59:10 +0000 | [diff] [blame^] | 413 | << NumIdentifiers/(double)HashTableSize << "\n"; |
| 414 | std::cerr << "Num probed identifiers: " << NumProbed << " (" |
| 415 | << NumProbed*100.0/NumIdentifiers << "%)\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 416 | std::cerr << "Ave identifier length: " |
| 417 | << (AverageIdentifierSize/(double)NumIdentifiers) << "\n"; |
| 418 | std::cerr << "Max identifier length: " << MaxIdentifierLength << "\n"; |
| 419 | |
| 420 | // Compute statistics about the memory allocated for identifiers. |
| 421 | #if USE_ALLOCATOR |
| 422 | unsigned BytesUsed = 0; |
| 423 | unsigned NumRegions = 0; |
| 424 | const MemRegion *R = (MemRegion*)TheMemory; |
| 425 | for (; R; R = R->getNext(), ++NumRegions) { |
| 426 | BytesUsed += R->getNumBytesAllocated(); |
| 427 | } |
| 428 | std::cerr << "\nNumber of memory regions: " << NumRegions << "\n"; |
| 429 | std::cerr << "Bytes allocated for identifiers: " << BytesUsed << "\n"; |
| 430 | #endif |
| 431 | } |
| 432 | |
| 433 | |