simon_tatham created this revision. simon_tatham added reviewers: rsmith, lebedev.ri, akyrtzi. Herald added subscribers: dexonsmith, arphaman, kbarton, nemanjai. simon_tatham requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This is part of a patch series working towards the ability to make SourceLocation into a 64-bit type to handle larger translation units. NFC: this patch introduces typedefs for the integer type used by SourceLocation and makes all the boring changes to use the typedefs everywhere, but for the moment, they are unconditionally defined to uint32_t. Patch originally by Mikhail Maltsev. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D105492 Files: clang/include/clang/AST/DeclarationName.h clang/include/clang/Basic/SourceLocation.h clang/include/clang/Basic/SourceManager.h clang/include/clang/Lex/Token.h clang/include/clang/Serialization/ASTBitCodes.h clang/include/clang/Serialization/ASTReader.h clang/include/clang/Serialization/ASTWriter.h clang/include/clang/Serialization/ModuleFile.h clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp clang/lib/AST/NestedNameSpecifier.cpp clang/lib/Basic/SourceLocation.cpp clang/lib/Basic/SourceManager.cpp clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/lib/Lex/Lexer.cpp clang/lib/Lex/ModuleMap.cpp clang/lib/Lex/PPCaching.cpp clang/lib/Lex/TokenLexer.cpp clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTWriter.cpp clang/tools/libclang/CIndex.cpp
Index: clang/tools/libclang/CIndex.cpp =================================================================== --- clang/tools/libclang/CIndex.cpp +++ clang/tools/libclang/CIndex.cpp @@ -1982,7 +1982,8 @@ return static_cast<const FieldDecl *>(data[0]); } SourceLocation getLoc() const { - return SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)data[1]); + return SourceLocation::getFromRawEncoding( + (SourceLocation::UIntType)(uintptr_t)data[1]); } }; class EnqueueVisitor : public ConstStmtVisitor<EnqueueVisitor, void> { Index: clang/lib/Serialization/ASTWriter.cpp =================================================================== --- clang/lib/Serialization/ASTWriter.cpp +++ clang/lib/Serialization/ASTWriter.cpp @@ -2041,7 +2041,7 @@ Record.push_back(Expansion.isExpansionTokenRange()); // Compute the token length for this macro expansion. - unsigned NextOffset = SourceMgr.getNextLocalOffset(); + SourceLocation::UIntType NextOffset = SourceMgr.getNextLocalOffset(); if (I + 1 != N) NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset(); Record.push_back(NextOffset - SLoc->getOffset() - 1); @@ -4640,7 +4640,7 @@ // another module after it or have more than one entity inside it. uint32_t None = std::numeric_limits<uint32_t>::max(); - auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) { + auto writeBaseIDOrNone = [&](auto BaseID, bool ShouldWrite) { assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high"); if (ShouldWrite) LE.write<uint32_t>(BaseID); @@ -5027,8 +5027,8 @@ } void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) { - uint32_t Raw = Loc.getRawEncoding(); - Record.push_back((Raw << 1) | (Raw >> 31)); + SourceLocation::UIntType Raw = Loc.getRawEncoding(); + Record.push_back((Raw << 1) | (Raw >> (8 * sizeof(Raw) - 1))); } void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) { Index: clang/lib/Serialization/ASTReader.cpp =================================================================== --- clang/lib/Serialization/ASTReader.cpp +++ clang/lib/Serialization/ASTReader.cpp @@ -1482,7 +1482,7 @@ } BitstreamCursor &SLocEntryCursor = F->SLocEntryCursor; - unsigned BaseOffset = F->SLocEntryBaseOffset; + SourceLocation::UIntType BaseOffset = F->SLocEntryBaseOffset; ++NumSLocEntriesRead; Expected<llvm::BitstreamEntry> MaybeEntry = SLocEntryCursor.advance(); @@ -3404,7 +3404,7 @@ case SOURCE_LOCATION_OFFSETS: { F.SLocEntryOffsets = (const uint32_t *)Blob.data(); F.LocalNumSLocEntries = Record[0]; - unsigned SLocSpaceSize = Record[1]; + SourceLocation::UIntType SLocSpaceSize = Record[1]; F.SLocEntryOffsetsBase = Record[2] + F.SourceManagerBlockStartOffset; std::tie(F.SLocEntryBaseID, F.SLocEntryBaseOffset) = SourceMgr.AllocateLoadedSLocEntries(F.LocalNumSLocEntries, @@ -3422,7 +3422,7 @@ F.FirstLoc = SourceLocation::getFromRawEncoding(F.SLocEntryBaseOffset); // SLocEntryBaseOffset is lower than MaxLoadedOffset and decreasing. - assert((F.SLocEntryBaseOffset & (1U << 31U)) == 0); + assert((F.SLocEntryBaseOffset & SourceLocation::MacroIDBit) == 0); GlobalSLocOffsetMap.insert( std::make_pair(SourceManager::MaxLoadedOffset - F.SLocEntryBaseOffset - SLocSpaceSize,&F)); @@ -3431,8 +3431,8 @@ // Invalid stays invalid. F.SLocRemap.insertOrReplace(std::make_pair(0U, 0)); // This module. Base was 2 when being compiled. - F.SLocRemap.insertOrReplace(std::make_pair(2U, - static_cast<int>(F.SLocEntryBaseOffset - 2))); + F.SLocRemap.insertOrReplace(std::make_pair( + 2U, static_cast<SourceLocation::IntType>(F.SLocEntryBaseOffset - 2))); TotalNumSLocEntries += F.LocalNumSLocEntries; break; @@ -3833,8 +3833,11 @@ } // Continuous range maps we may be updating in our module. + using SLocRemapBuilder = + ContinuousRangeMap<SourceLocation::UIntType, SourceLocation::IntType, + 2>::Builder; using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder; - RemapBuilder SLocRemap(F.SLocRemap); + SLocRemapBuilder SLocRemap(F.SLocRemap); RemapBuilder IdentifierRemap(F.IdentifierRemap); RemapBuilder MacroRemap(F.MacroRemap); RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap); @@ -3865,7 +3868,7 @@ return; } - uint32_t SLocOffset = + SourceLocation::UIntType SLocOffset = endian::readNext<uint32_t, little, unaligned>(Data); uint32_t IdentifierIDOffset = endian::readNext<uint32_t, little, unaligned>(Data); @@ -3882,15 +3885,21 @@ uint32_t TypeIndexOffset = endian::readNext<uint32_t, little, unaligned>(Data); - uint32_t None = std::numeric_limits<uint32_t>::max(); - auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset, RemapBuilder &Remap) { + constexpr uint32_t None = std::numeric_limits<uint32_t>::max(); if (Offset != None) Remap.insert(std::make_pair(Offset, static_cast<int>(BaseOffset - Offset))); }; - mapOffset(SLocOffset, OM->SLocEntryBaseOffset, SLocRemap); + + constexpr SourceLocation::UIntType SLocNone = + std::numeric_limits<SourceLocation::UIntType>::max(); + if (SLocOffset != SLocNone) + SLocRemap.insert(std::make_pair( + SLocOffset, static_cast<SourceLocation::IntType>( + OM->SLocEntryBaseOffset - SLocOffset))); + mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap); mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap); mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID, Index: clang/lib/Lex/TokenLexer.cpp =================================================================== --- clang/lib/Lex/TokenLexer.cpp +++ clang/lib/Lex/TokenLexer.cpp @@ -971,7 +971,7 @@ assert(SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength) && "Expected loc to come from the macro definition"); - unsigned relativeOffset = 0; + SourceLocation::UIntType relativeOffset = 0; SM.isInSLocAddrSpace(loc, MacroDefStart, MacroDefLength, &relativeOffset); return MacroExpansionStart.getLocWithOffset(relativeOffset); } @@ -1010,7 +1010,7 @@ if (CurLoc.isFileID() != NextLoc.isFileID()) break; // Token from different kind of FileID. - int RelOffs; + SourceLocation::IntType RelOffs; if (!SM.isInSameSLocAddrSpace(CurLoc, NextLoc, &RelOffs)) break; // Token from different local/loaded location. // Check that token is not before the previous token or more than 50 @@ -1027,10 +1027,11 @@ // For the consecutive tokens, find the length of the SLocEntry to contain // all of them. Token &LastConsecutiveTok = *(NextTok-1); - int LastRelOffs = 0; + SourceLocation::IntType LastRelOffs = 0; SM.isInSameSLocAddrSpace(FirstLoc, LastConsecutiveTok.getLocation(), &LastRelOffs); - unsigned FullLength = LastRelOffs + LastConsecutiveTok.getLength(); + SourceLocation::UIntType FullLength = + LastRelOffs + LastConsecutiveTok.getLength(); // Create a macro expansion SLocEntry that will "contain" all of the tokens. SourceLocation Expansion = @@ -1040,7 +1041,7 @@ // expanded location. for (; begin_tokens < NextTok; ++begin_tokens) { Token &Tok = *begin_tokens; - int RelOffs = 0; + SourceLocation::IntType RelOffs = 0; SM.isInSameSLocAddrSpace(FirstLoc, Tok.getLocation(), &RelOffs); Tok.setLocation(Expansion.getLocWithOffset(RelOffs)); } Index: clang/lib/Lex/PPCaching.cpp =================================================================== --- clang/lib/Lex/PPCaching.cpp +++ clang/lib/Lex/PPCaching.cpp @@ -145,7 +145,7 @@ if (LastCachedTok.getKind() != Tok.getKind()) return false; - int RelOffset = 0; + SourceLocation::IntType RelOffset = 0; if ((!getSourceManager().isInSameSLocAddrSpace( Tok.getLocation(), getLastCachedTokenLocation(), &RelOffset)) || RelOffset) Index: clang/lib/Lex/ModuleMap.cpp =================================================================== --- clang/lib/Lex/ModuleMap.cpp +++ clang/lib/Lex/ModuleMap.cpp @@ -1378,7 +1378,7 @@ RSquare } Kind; - unsigned Location; + SourceLocation::UIntType Location; unsigned StringLength; union { // If Kind != IntegerLiteral. Index: clang/lib/Lex/Lexer.cpp =================================================================== --- clang/lib/Lex/Lexer.cpp +++ clang/lib/Lex/Lexer.cpp @@ -588,7 +588,7 @@ // Create a lexer starting at the beginning of the file. Note that we use a // "fake" file source location at offset 1 so that the lexer will track our // position within the file. - const unsigned StartOffset = 1; + const SourceLocation::UIntType StartOffset = 1; SourceLocation FileLoc = SourceLocation::getFromRawEncoding(StartOffset); Lexer TheLexer(FileLoc, LangOpts, Buffer.begin(), Buffer.begin(), Buffer.end()); Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -12037,13 +12037,14 @@ // Cleanup action for allocate support. class OMPAllocateCleanupTy final : public EHScopeStack::Cleanup { llvm::FunctionCallee RTLFn; - unsigned LocEncoding; + SourceLocation::UIntType LocEncoding; Address Addr; const Expr *Allocator; public: - OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn, unsigned LocEncoding, - Address Addr, const Expr *Allocator) + OMPAllocateCleanupTy(llvm::FunctionCallee RTLFn, + SourceLocation::UIntType LocEncoding, Address Addr, + const Expr *Allocator) : RTLFn(RTLFn), LocEncoding(LocEncoding), Addr(Addr), Allocator(Allocator) {} void Emit(CodeGenFunction &CGF, Flags /*flags*/) override { Index: clang/lib/Basic/SourceManager.cpp =================================================================== --- clang/lib/Basic/SourceManager.cpp +++ clang/lib/Basic/SourceManager.cpp @@ -450,9 +450,9 @@ return LoadedSLocEntryTable[Index]; } -std::pair<int, unsigned> +std::pair<int, SourceLocation::UIntType> SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries, - unsigned TotalSize) { + SourceLocation::UIntType TotalSize) { assert(ExternalSLocEntries && "Don't have an external sloc source"); // Make sure we're not about to run out of source locations. if (CurrentLoadedOffset - TotalSize < NextLocalOffset) @@ -532,7 +532,8 @@ FileID SourceManager::createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset) { + int LoadedID, + SourceLocation::UIntType LoadedOffset) { return createFileID(SourceFile->getLastRef(), IncludePos, FileCharacter, LoadedID, LoadedOffset); } @@ -540,7 +541,8 @@ FileID SourceManager::createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset) { + int LoadedID, + SourceLocation::UIntType LoadedOffset) { SrcMgr::ContentCache &IR = getOrCreateContentCache(SourceFile, isSystem(FileCharacter)); @@ -559,7 +561,8 @@ /// MemoryBuffer, so only pass a MemoryBuffer to this once. FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset, + int LoadedID, + SourceLocation::UIntType LoadedOffset, SourceLocation IncludeLoc) { StringRef Name = Buffer->getBufferIdentifier(); return createFileIDImpl(createMemBufferContentCache(std::move(Buffer)), Name, @@ -572,7 +575,8 @@ /// outlive the SourceManager. FileID SourceManager::createFileID(const llvm::MemoryBufferRef &Buffer, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset, + int LoadedID, + SourceLocation::UIntType LoadedOffset, SourceLocation IncludeLoc) { return createFileID(llvm::MemoryBuffer::getMemBuffer(Buffer), FileCharacter, LoadedID, LoadedOffset, IncludeLoc); @@ -594,7 +598,8 @@ FileID SourceManager::createFileIDImpl(ContentCache &File, StringRef Filename, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset) { + int LoadedID, + SourceLocation::UIntType LoadedOffset) { if (LoadedID < 0) { assert(LoadedID != -1 && "Loading sentinel FileID"); unsigned Index = unsigned(-LoadedID) - 2; @@ -633,14 +638,11 @@ return createExpansionLocImpl(Info, TokLength); } -SourceLocation -SourceManager::createExpansionLoc(SourceLocation SpellingLoc, - SourceLocation ExpansionLocStart, - SourceLocation ExpansionLocEnd, - unsigned TokLength, - bool ExpansionIsTokenRange, - int LoadedID, - unsigned LoadedOffset) { +SourceLocation SourceManager::createExpansionLoc( + SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, + SourceLocation ExpansionLocEnd, unsigned TokLength, + bool ExpansionIsTokenRange, int LoadedID, + SourceLocation::UIntType LoadedOffset) { ExpansionInfo Info = ExpansionInfo::create( SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange); return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset); @@ -658,9 +660,8 @@ SourceLocation SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, - unsigned TokLength, - int LoadedID, - unsigned LoadedOffset) { + unsigned TokLength, int LoadedID, + SourceLocation::UIntType LoadedOffset) { if (LoadedID < 0) { assert(LoadedID != -1 && "Loading sentinel FileID"); unsigned Index = unsigned(-LoadedID) - 2; @@ -762,7 +763,7 @@ /// This is the cache-miss path of getFileID. Not as hot as that function, but /// still very important. It is responsible for finding the entry in the /// SLocEntry tables that contains the specified location. -FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { +FileID SourceManager::getFileIDSlow(SourceLocation::UIntType SLocOffset) const { if (!SLocOffset) return FileID::get(0); @@ -777,7 +778,8 @@ /// /// This function knows that the SourceLocation is in a local buffer, not a /// loaded one. -FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const { +FileID +SourceManager::getFileIDLocal(SourceLocation::UIntType SLocOffset) const { assert(SLocOffset < NextLocalOffset && "Bad function choice"); // After the first and second level caches, I see two common sorts of @@ -828,7 +830,8 @@ NumProbes = 0; while (true) { unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; - unsigned MidOffset = getLocalSLocEntry(MiddleIndex).getOffset(); + SourceLocation::UIntType MidOffset = + getLocalSLocEntry(MiddleIndex).getOffset(); ++NumProbes; @@ -859,7 +862,8 @@ /// /// This function knows that the SourceLocation is in a loaded buffer, not a /// local one. -FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const { +FileID +SourceManager::getFileIDLoaded(SourceLocation::UIntType SLocOffset) const { // Sanity checking, otherwise a bug may lead to hanging in release build. if (SLocOffset < CurrentLoadedOffset) { assert(0 && "Invalid SLocOffset or bad function choice"); @@ -1619,7 +1623,7 @@ return 0; int ID = FID.ID; - unsigned NextOffset; + SourceLocation::UIntType NextOffset; if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size())) NextOffset = getNextLocalOffset(); else if (ID+1 == -1) @@ -1827,8 +1831,8 @@ SourceLocation ExpansionLoc, unsigned ExpansionLength) const { if (!SpellLoc.isFileID()) { - unsigned SpellBeginOffs = SpellLoc.getOffset(); - unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength; + SourceLocation::UIntType SpellBeginOffs = SpellLoc.getOffset(); + SourceLocation::UIntType SpellEndOffs = SpellBeginOffs + ExpansionLength; // The spelling range for this macro argument expansion can span multiple // consecutive FileID entries. Go through each entry contained in the @@ -1840,9 +1844,10 @@ std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc); while (true) { const SLocEntry &Entry = getSLocEntry(SpellFID); - unsigned SpellFIDBeginOffs = Entry.getOffset(); + SourceLocation::UIntType SpellFIDBeginOffs = Entry.getOffset(); unsigned SpellFIDSize = getFileIDSize(SpellFID); - unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize; + SourceLocation::UIntType SpellFIDEndOffs = + SpellFIDBeginOffs + SpellFIDSize; const ExpansionInfo &Info = Entry.getExpansion(); if (Info.isMacroArgExpansion()) { unsigned CurrSpellLength; @@ -1934,7 +1939,7 @@ --I; - unsigned MacroArgBeginOffs = I->first; + SourceLocation::UIntType MacroArgBeginOffs = I->first; SourceLocation MacroArgExpandedLoc = I->second; if (MacroArgExpandedLoc.isValid()) return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs); @@ -2154,7 +2159,7 @@ llvm::raw_ostream &out = llvm::errs(); auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry, - llvm::Optional<unsigned> NextStart) { + llvm::Optional<SourceLocation::UIntType> NextStart) { out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion") << " <SourceLocation " << Entry.getOffset() << ":"; if (NextStart) @@ -2194,7 +2199,7 @@ : LocalSLocEntryTable[ID + 1].getOffset()); } // Dump loaded SLocEntries. - llvm::Optional<unsigned> NextStart; + llvm::Optional<SourceLocation::UIntType> NextStart; for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) { int ID = -(int)Index - 2; if (SLocEntryLoaded[Index]) { Index: clang/lib/Basic/SourceLocation.cpp =================================================================== --- clang/lib/Basic/SourceLocation.cpp +++ clang/lib/Basic/SourceLocation.cpp @@ -51,7 +51,7 @@ "used in unions"); unsigned SourceLocation::getHashValue() const { - return llvm::DenseMapInfo<unsigned>::getHashValue(ID); + return llvm::DenseMapInfo<UIntType>::getHashValue(ID); } void llvm::FoldingSetTrait<SourceLocation>::Profile( Index: clang/lib/AST/NestedNameSpecifier.cpp =================================================================== --- clang/lib/AST/NestedNameSpecifier.cpp +++ clang/lib/AST/NestedNameSpecifier.cpp @@ -356,7 +356,7 @@ assert(Qualifier && "Expected a non-NULL qualifier"); // Location of the trailing '::'. - unsigned Length = sizeof(unsigned); + unsigned Length = sizeof(SourceLocation::UIntType); switch (Qualifier->getKind()) { case NestedNameSpecifier::Global: @@ -368,7 +368,7 @@ case NestedNameSpecifier::NamespaceAlias: case NestedNameSpecifier::Super: // The location of the identifier or namespace name. - Length += sizeof(unsigned); + Length += sizeof(SourceLocation::UIntType); break; case NestedNameSpecifier::TypeSpecWithTemplate: @@ -393,8 +393,8 @@ /// Load a (possibly unaligned) source location from a given address /// and offset. static SourceLocation LoadSourceLocation(void *Data, unsigned Offset) { - unsigned Raw; - memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(unsigned)); + SourceLocation::UIntType Raw; + memcpy(&Raw, static_cast<char *>(Data) + Offset, sizeof(Raw)); return SourceLocation::getFromRawEncoding(Raw); } @@ -431,8 +431,9 @@ case NestedNameSpecifier::Namespace: case NestedNameSpecifier::NamespaceAlias: case NestedNameSpecifier::Super: - return SourceRange(LoadSourceLocation(Data, Offset), - LoadSourceLocation(Data, Offset + sizeof(unsigned))); + return SourceRange( + LoadSourceLocation(Data, Offset), + LoadSourceLocation(Data, Offset + sizeof(SourceLocation::UIntType))); case NestedNameSpecifier::TypeSpecWithTemplate: case NestedNameSpecifier::TypeSpec: { @@ -487,10 +488,10 @@ /// Save a source location to the given buffer. static void SaveSourceLocation(SourceLocation Loc, char *&Buffer, unsigned &BufferSize, unsigned &BufferCapacity) { - unsigned Raw = Loc.getRawEncoding(); + SourceLocation::UIntType Raw = Loc.getRawEncoding(); Append(reinterpret_cast<char *>(&Raw), - reinterpret_cast<char *>(&Raw) + sizeof(unsigned), - Buffer, BufferSize, BufferCapacity); + reinterpret_cast<char *>(&Raw) + sizeof(Raw), Buffer, BufferSize, + BufferCapacity); } /// Save a pointer to the given buffer. Index: clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp =================================================================== --- clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp +++ clang/lib/ARCMigrate/TransEmptyStatementsAndDealloc.cpp @@ -52,7 +52,7 @@ if (AfterMacroLoc == SemiLoc) return true; - int RelOffs = 0; + SourceLocation::IntType RelOffs = 0; if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs)) return false; if (RelOffs < 0) Index: clang/include/clang/Serialization/ModuleFile.h =================================================================== --- clang/include/clang/Serialization/ModuleFile.h +++ clang/include/clang/Serialization/ModuleFile.h @@ -260,7 +260,7 @@ int SLocEntryBaseID = 0; /// The base offset in the source manager's view of this module. - unsigned SLocEntryBaseOffset = 0; + SourceLocation::UIntType SLocEntryBaseOffset = 0; /// Base file offset for the offsets in SLocEntryOffsets. Real file offset /// for the entry is SLocEntryOffsetsBase + SLocEntryOffsets[i]. @@ -274,7 +274,8 @@ SmallVector<uint64_t, 4> PreloadSLocEntries; /// Remapping table for source locations in this module. - ContinuousRangeMap<uint32_t, int, 2> SLocRemap; + ContinuousRangeMap<SourceLocation::UIntType, SourceLocation::IntType, 2> + SLocRemap; // === Identifiers === Index: clang/include/clang/Serialization/ASTWriter.h =================================================================== --- clang/include/clang/Serialization/ASTWriter.h +++ clang/include/clang/Serialization/ASTWriter.h @@ -347,7 +347,7 @@ union { const Decl *Dcl; void *Type; - unsigned Loc; + SourceLocation::UIntType Loc; unsigned Val; Module *Mod; const Attr *Attribute; Index: clang/include/clang/Serialization/ASTReader.h =================================================================== --- clang/include/clang/Serialization/ASTReader.h +++ clang/include/clang/Serialization/ASTReader.h @@ -2136,12 +2136,15 @@ /// Read a source location from raw form and return it in its /// originating module file's source location space. - SourceLocation ReadUntranslatedSourceLocation(uint32_t Raw) const { - return SourceLocation::getFromRawEncoding((Raw >> 1) | (Raw << 31)); + SourceLocation + ReadUntranslatedSourceLocation(SourceLocation::UIntType Raw) const { + return SourceLocation::getFromRawEncoding((Raw >> 1) | + (Raw << (8 * sizeof(Raw) - 1))); } /// Read a source location from raw form. - SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, uint32_t Raw) const { + SourceLocation ReadSourceLocation(ModuleFile &ModuleFile, + SourceLocation::UIntType Raw) const { SourceLocation Loc = ReadUntranslatedSourceLocation(Raw); return TranslateSourceLocation(ModuleFile, Loc); } @@ -2155,7 +2158,8 @@ assert(ModuleFile.SLocRemap.find(Loc.getOffset()) != ModuleFile.SLocRemap.end() && "Cannot find offset to remap."); - int Remap = ModuleFile.SLocRemap.find(Loc.getOffset())->second; + SourceLocation::IntType Remap = + ModuleFile.SLocRemap.find(Loc.getOffset())->second; return Loc.getLocWithOffset(Remap); } Index: clang/include/clang/Serialization/ASTBitCodes.h =================================================================== --- clang/include/clang/Serialization/ASTBitCodes.h +++ clang/include/clang/Serialization/ASTBitCodes.h @@ -176,10 +176,10 @@ /// Source range/offset of a preprocessed entity. struct PPEntityOffset { /// Raw source location of beginning of range. - unsigned Begin; + SourceLocation::UIntType Begin; /// Raw source location of end of range. - unsigned End; + SourceLocation::UIntType End; /// Offset in the AST file relative to ModuleFile::MacroOffsetsBase. uint32_t BitOffset; @@ -200,9 +200,9 @@ /// Source range of a skipped preprocessor region struct PPSkippedRange { /// Raw source location of beginning of range. - unsigned Begin; + SourceLocation::UIntType Begin; /// Raw source location of end of range. - unsigned End; + SourceLocation::UIntType End; PPSkippedRange(SourceRange R) : Begin(R.getBegin().getRawEncoding()), End(R.getEnd().getRawEncoding()) { @@ -240,7 +240,7 @@ /// Source location and bit offset of a declaration. struct DeclOffset { /// Raw source location. - unsigned Loc = 0; + SourceLocation::UIntType Loc = 0; /// Offset relative to the start of the DECLTYPES_BLOCK block. Keep /// structure alignment 32-bit and avoid padding gap because undefined Index: clang/include/clang/Lex/Token.h =================================================================== --- clang/include/clang/Lex/Token.h +++ clang/include/clang/Lex/Token.h @@ -33,7 +33,7 @@ /// information about the SourceRange of the tokens and the type object. class Token { /// The location of the token. This is actually a SourceLocation. - unsigned Loc; + SourceLocation::UIntType Loc; // Conceptually these next two fields could be in a union. However, this // causes gcc 4.2 to pessimize LexTokenInternal, a very performance critical @@ -43,7 +43,7 @@ /// UintData - This holds either the length of the token text, when /// a normal token, or the end of the SourceRange when an annotation /// token. - unsigned UintData; + SourceLocation::UIntType UintData; /// PtrData - This is a union of four different pointer types, which depends /// on what type of token this is: Index: clang/include/clang/Basic/SourceManager.h =================================================================== --- clang/include/clang/Basic/SourceManager.h +++ clang/include/clang/Basic/SourceManager.h @@ -465,8 +465,9 @@ /// SourceManager keeps an array of these objects, and they are uniquely /// identified by the FileID datatype. class SLocEntry { - unsigned Offset : 31; - unsigned IsExpansion : 1; + static constexpr int OffsetBits = 8 * sizeof(SourceLocation::UIntType) - 1; + SourceLocation::UIntType Offset : OffsetBits; + SourceLocation::UIntType IsExpansion : 1; union { FileInfo File; ExpansionInfo Expansion; @@ -475,7 +476,7 @@ public: SLocEntry() : Offset(), IsExpansion(), File() {} - unsigned getOffset() const { return Offset; } + SourceLocation::UIntType getOffset() const { return Offset; } bool isExpansion() const { return IsExpansion; } bool isFile() const { return !isExpansion(); } @@ -490,8 +491,8 @@ return Expansion; } - static SLocEntry get(unsigned Offset, const FileInfo &FI) { - assert(!(Offset & (1u << 31)) && "Offset is too large"); + static SLocEntry get(SourceLocation::UIntType Offset, const FileInfo &FI) { + assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large"); SLocEntry E; E.Offset = Offset; E.IsExpansion = false; @@ -499,8 +500,9 @@ return E; } - static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { - assert(!(Offset & (1u << 31)) && "Offset is too large"); + static SLocEntry get(SourceLocation::UIntType Offset, + const ExpansionInfo &Expansion) { + assert(!(Offset & (1ULL << OffsetBits)) && "Offset is too large"); SLocEntry E; E.Offset = Offset; E.IsExpansion = true; @@ -690,17 +692,18 @@ /// The starting offset of the next local SLocEntry. /// /// This is LocalSLocEntryTable.back().Offset + the size of that entry. - unsigned NextLocalOffset; + SourceLocation::UIntType NextLocalOffset; /// The starting offset of the latest batch of loaded SLocEntries. /// /// This is LoadedSLocEntryTable.back().Offset, except that that entry might /// not have been loaded, so that value would be unknown. - unsigned CurrentLoadedOffset; + SourceLocation::UIntType CurrentLoadedOffset; - /// The highest possible offset is 2^31-1, so CurrentLoadedOffset - /// starts at 2^31. - static const unsigned MaxLoadedOffset = 1U << 31U; + /// The highest possible offset is 2^32-1 (2^63-1 for 64-bit source + /// locations), so CurrentLoadedOffset starts at 2^31 (2^63 resp.). + static const SourceLocation::UIntType MaxLoadedOffset = + 1ULL << (8 * sizeof(SourceLocation::UIntType) - 1); /// A bitmap that indicates whether the entries of LoadedSLocEntryTable /// have already been loaded from the external source. @@ -865,11 +868,13 @@ /// This translates NULL into standard input. FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID = 0, unsigned LoadedOffset = 0); + int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0); FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, - int LoadedID = 0, unsigned LoadedOffset = 0); + int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0); /// Create a new FileID that represents the specified memory buffer. /// @@ -877,7 +882,8 @@ /// MemoryBuffer, so only pass a MemoryBuffer to this once. FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer, SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User, - int LoadedID = 0, unsigned LoadedOffset = 0, + int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0, SourceLocation IncludeLoc = SourceLocation()); /// Create a new FileID that represents the specified memory buffer. @@ -886,7 +892,8 @@ /// outlive the SourceManager. FileID createFileID(const llvm::MemoryBufferRef &Buffer, SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User, - int LoadedID = 0, unsigned LoadedOffset = 0, + int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0, SourceLocation IncludeLoc = SourceLocation()); /// Get the FileID for \p SourceFile if it exists. Otherwise, create a @@ -905,13 +912,11 @@ /// Return a new SourceLocation that encodes the fact /// that a token from SpellingLoc should actually be referenced from /// ExpansionLoc. - SourceLocation createExpansionLoc(SourceLocation Loc, - SourceLocation ExpansionLocStart, - SourceLocation ExpansionLocEnd, - unsigned TokLength, - bool ExpansionIsTokenRange = true, - int LoadedID = 0, - unsigned LoadedOffset = 0); + SourceLocation + createExpansionLoc(SourceLocation Loc, SourceLocation ExpansionLocStart, + SourceLocation ExpansionLocEnd, unsigned TokLength, + bool ExpansionIsTokenRange = true, int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0); /// Return a new SourceLocation that encodes that the token starting /// at \p TokenStart ends prematurely at \p TokenEnd. @@ -1098,7 +1103,7 @@ /// the entry in SLocEntryTable which contains the specified location. /// FileID getFileID(SourceLocation SpellingLoc) const { - unsigned SLocOffset = SpellingLoc.getOffset(); + SourceLocation::UIntType SLocOffset = SpellingLoc.getOffset(); // If our one-entry cache covers this offset, just return it. if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) @@ -1221,7 +1226,7 @@ if (!Entry) return SourceLocation(); - unsigned GlobalOffset = Entry->getOffset() + Offset; + SourceLocation::UIntType GlobalOffset = Entry->getOffset() + Offset; return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset) : SourceLocation::getMacroLoc(GlobalOffset); } @@ -1326,17 +1331,17 @@ /// /// If it's true and \p RelativeOffset is non-null, it will be set to the /// relative offset of \p Loc inside the chunk. - bool isInSLocAddrSpace(SourceLocation Loc, - SourceLocation Start, unsigned Length, - unsigned *RelativeOffset = nullptr) const { + bool + isInSLocAddrSpace(SourceLocation Loc, SourceLocation Start, unsigned Length, + SourceLocation::UIntType *RelativeOffset = nullptr) const { assert(((Start.getOffset() < NextLocalOffset && Start.getOffset()+Length <= NextLocalOffset) || (Start.getOffset() >= CurrentLoadedOffset && Start.getOffset()+Length < MaxLoadedOffset)) && "Chunk is not valid SLoc address space"); - unsigned LocOffs = Loc.getOffset(); - unsigned BeginOffs = Start.getOffset(); - unsigned EndOffs = BeginOffs + Length; + SourceLocation::UIntType LocOffs = Loc.getOffset(); + SourceLocation::UIntType BeginOffs = Start.getOffset(); + SourceLocation::UIntType EndOffs = BeginOffs + Length; if (LocOffs >= BeginOffs && LocOffs < EndOffs) { if (RelativeOffset) *RelativeOffset = LocOffs - BeginOffs; @@ -1352,8 +1357,9 @@ /// If it's true and \p RelativeOffset is non-null, it will be set to the /// offset of \p RHS relative to \p LHS. bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, - int *RelativeOffset) const { - unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); + SourceLocation::IntType *RelativeOffset) const { + SourceLocation::UIntType LHSOffs = LHS.getOffset(), + RHSOffs = RHS.getOffset(); bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; @@ -1517,7 +1523,7 @@ /// of FileID) to \p relativeOffset. bool isInFileID(SourceLocation Loc, FileID FID, unsigned *RelativeOffset = nullptr) const { - unsigned Offs = Loc.getOffset(); + SourceLocation::UIntType Offs = Loc.getOffset(); if (isOffsetInFileID(FID, Offs)) { if (RelativeOffset) *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); @@ -1636,8 +1642,9 @@ /// offset in the "source location address space". /// /// Note that we always consider source locations loaded from - bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { - unsigned LHSOffset = LHS.getOffset(); + bool isBeforeInSLocAddrSpace(SourceLocation LHS, + SourceLocation::UIntType RHS) const { + SourceLocation::UIntType LHSOffset = LHS.getOffset(); bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; bool RHSLoaded = RHS >= CurrentLoadedOffset; if (LHSLoaded == RHSLoaded) @@ -1699,7 +1706,9 @@ return getSLocEntryByID(FID.ID, Invalid); } - unsigned getNextLocalOffset() const { return NextLocalOffset; } + SourceLocation::UIntType getNextLocalOffset() const { + return NextLocalOffset; + } void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { assert(LoadedSLocEntryTable.empty() && @@ -1713,8 +1722,9 @@ /// NumSLocEntries will be allocated, which occupy a total of TotalSize space /// in the global source view. The lowest ID and the base offset of the /// entries will be returned. - std::pair<int, unsigned> - AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); + std::pair<int, SourceLocation::UIntType> + AllocateLoadedSLocEntries(unsigned NumSLocEntries, + SourceLocation::UIntType TotalSize); /// Returns true if \p Loc came from a PCH/Module. bool isLoadedSourceLocation(SourceLocation Loc) const { @@ -1795,14 +1805,15 @@ /// Implements the common elements of storing an expansion info struct into /// the SLocEntry table and producing a source location that refers to it. - SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, - unsigned TokLength, - int LoadedID = 0, - unsigned LoadedOffset = 0); + SourceLocation + createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, + unsigned TokLength, int LoadedID = 0, + SourceLocation::UIntType LoadedOffset = 0); /// Return true if the specified FileID contains the /// specified SourceLocation offset. This is a very hot method. - inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { + inline bool isOffsetInFileID(FileID FID, + SourceLocation::UIntType SLocOffset) const { const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); // If the entry is after the offset, it can't contain it. if (SLocOffset < Entry.getOffset()) return false; @@ -1836,7 +1847,7 @@ FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename, SourceLocation IncludePos, SrcMgr::CharacteristicKind DirCharacter, int LoadedID, - unsigned LoadedOffset); + SourceLocation::UIntType LoadedOffset); SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile, bool isSystemFile = false); @@ -1845,9 +1856,9 @@ SrcMgr::ContentCache & createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf); - FileID getFileIDSlow(unsigned SLocOffset) const; - FileID getFileIDLocal(unsigned SLocOffset) const; - FileID getFileIDLoaded(unsigned SLocOffset) const; + FileID getFileIDSlow(SourceLocation::UIntType SLocOffset) const; + FileID getFileIDLocal(SourceLocation::UIntType SLocOffset) const; + FileID getFileIDLoaded(SourceLocation::UIntType SLocOffset) const; SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; Index: clang/include/clang/Basic/SourceLocation.h =================================================================== --- clang/include/clang/Basic/SourceLocation.h +++ clang/include/clang/Basic/SourceLocation.h @@ -92,11 +92,14 @@ friend class SourceManager; friend struct llvm::FoldingSetTrait<SourceLocation>; - unsigned ID = 0; +public: + using UIntType = uint32_t; + using IntType = int32_t; - enum : unsigned { - MacroIDBit = 1U << 31 - }; +private: + UIntType ID = 0; + + enum : UIntType { MacroIDBit = 1ULL << (8 * sizeof(UIntType) - 1) }; public: bool isFileID() const { return (ID & MacroIDBit) == 0; } @@ -112,18 +115,16 @@ private: /// Return the offset into the manager's global input view. - unsigned getOffset() const { - return ID & ~MacroIDBit; - } + UIntType getOffset() const { return ID & ~MacroIDBit; } - static SourceLocation getFileLoc(unsigned ID) { + static SourceLocation getFileLoc(UIntType ID) { assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); SourceLocation L; L.ID = ID; return L; } - static SourceLocation getMacroLoc(unsigned ID) { + static SourceLocation getMacroLoc(UIntType ID) { assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); SourceLocation L; L.ID = MacroIDBit | ID; @@ -133,7 +134,7 @@ public: /// Return a source location with the specified offset from this /// SourceLocation. - SourceLocation getLocWithOffset(int Offset) const { + SourceLocation getLocWithOffset(IntType Offset) const { assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow"); SourceLocation L; L.ID = ID+Offset; @@ -145,13 +146,13 @@ /// /// This should only be passed to SourceLocation::getFromRawEncoding, it /// should not be inspected directly. - unsigned getRawEncoding() const { return ID; } + UIntType getRawEncoding() const { return ID; } /// Turn a raw encoding of a SourceLocation object into /// a real SourceLocation. /// /// \see getRawEncoding. - static SourceLocation getFromRawEncoding(unsigned Encoding) { + static SourceLocation getFromRawEncoding(UIntType Encoding) { SourceLocation X; X.ID = Encoding; return X; @@ -171,7 +172,7 @@ /// Turn a pointer encoding of a SourceLocation object back /// into a real SourceLocation. static SourceLocation getFromPtrEncoding(const void *Encoding) { - return getFromRawEncoding((unsigned)(uintptr_t)Encoding); + return getFromRawEncoding((SourceLocation::UIntType)(uintptr_t)Encoding); } static bool isPairOfFileLocations(SourceLocation Start, SourceLocation End) { @@ -489,11 +490,13 @@ /// DenseMapInfo<unsigned> which uses SourceLocation::ID is used as a key. template <> struct DenseMapInfo<clang::SourceLocation> { static clang::SourceLocation getEmptyKey() { - return clang::SourceLocation::getFromRawEncoding(~0U); + constexpr clang::SourceLocation::UIntType Zero = 0; + return clang::SourceLocation::getFromRawEncoding(~Zero); } static clang::SourceLocation getTombstoneKey() { - return clang::SourceLocation::getFromRawEncoding(~0U - 1); + constexpr clang::SourceLocation::UIntType Zero = 0; + return clang::SourceLocation::getFromRawEncoding(~Zero - 1); } static unsigned getHashValue(clang::SourceLocation Loc) { Index: clang/include/clang/AST/DeclarationName.h =================================================================== --- clang/include/clang/AST/DeclarationName.h +++ clang/include/clang/AST/DeclarationName.h @@ -660,13 +660,13 @@ // The location (if any) of the operator keyword is stored elsewhere. struct CXXOpName { - unsigned BeginOpNameLoc; - unsigned EndOpNameLoc; + SourceLocation::UIntType BeginOpNameLoc; + SourceLocation::UIntType EndOpNameLoc; }; // The location (if any) of the operator keyword is stored elsewhere. struct CXXLitOpName { - unsigned OpNameLoc; + SourceLocation::UIntType OpNameLoc; }; // struct {} CXXUsingDirective;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits