From: Daniel Dunbar Date: Wed, 11 Nov 2009 05:26:28 +0000 (+0000) Subject: More StringRef simplification to PCHValidator::ReadPredefinesBuffer. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4d5936aaa4e4e2d41fe79101ac9c09444951448f;p=clang More StringRef simplification to PCHValidator::ReadPredefinesBuffer. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86805 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/PCHReader.h b/include/clang/Frontend/PCHReader.h index f9a31c1e45..0c56109f00 100644 --- a/include/clang/Frontend/PCHReader.h +++ b/include/clang/Frontend/PCHReader.h @@ -622,13 +622,15 @@ public: /// \brief Retrieve the IdentifierInfo for the named identifier. /// - /// This routine builds a new IdentifierInfo for the given - /// identifier. If any declarations with this name are visible from - /// translation unit scope, their declarations will be deserialized - /// and introduced into the declaration chain of the - /// identifier. FIXME: if this identifier names a macro, deserialize - /// the macro. + /// This routine builds a new IdentifierInfo for the given identifier. If any + /// declarations with this name are visible from translation unit scope, their + /// declarations will be deserialized and introduced into the declaration + /// chain of the identifier. FIXME: if this identifier names a macro, + /// deserialize the macro. virtual IdentifierInfo* get(const char *NameStart, const char *NameEnd); + IdentifierInfo* get(llvm::StringRef Name) { + return get(Name.begin(), Name.end()); + } /// \brief Load the contents of the global method pool for a given /// selector. diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index 641ab9f623..42aa43b81f 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -168,13 +168,14 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, std::vector CmdLineLines = splitLines(PP.getPredefines()); std::vector PCHLines = splitLines(PCHPredef); - // Sort both sets of predefined buffer lines, since + // Sort both sets of predefined buffer lines, since we allow some extra + // definitions and they may appear at any point in the output. std::sort(CmdLineLines.begin(), CmdLineLines.end()); std::sort(PCHLines.begin(), PCHLines.end()); - // Determine which predefines that where used to build the PCH file are - // missing from the command line. - std::vector MissingPredefines; + // Determine which predefines that were used to build the PCH file are missing + // from the command line. + std::vector MissingPredefines; std::set_difference(PCHLines.begin(), PCHLines.end(), CmdLineLines.begin(), CmdLineLines.end(), std::back_inserter(MissingPredefines)); @@ -182,8 +183,8 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, bool MissingDefines = false; bool ConflictingDefines = false; for (unsigned I = 0, N = MissingPredefines.size(); I != N; ++I) { - const std::string &Missing = MissingPredefines[I]; - if (!llvm::StringRef(Missing).startswith("#define ")) { + llvm::StringRef Missing = MissingPredefines[I]; + if (!Missing.startswith("#define ")) { Reader.Diag(diag::warn_pch_compiler_options_mismatch); return true; } @@ -195,12 +196,11 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, = Missing.find_first_of("( \n\r", StartOfMacroName); assert(EndOfMacroName != std::string::npos && "Couldn't find the end of the macro name"); - std::string MacroName = Missing.substr(StartOfMacroName, - EndOfMacroName - StartOfMacroName); + llvm::StringRef MacroName = Missing.slice(StartOfMacroName, EndOfMacroName); // Determine whether this macro was given a different definition on the // command line. - std::string MacroDefStart = "#define " + MacroName; + std::string MacroDefStart = "#define " + MacroName.str(); std::string::size_type MacroDefLen = MacroDefStart.size(); std::vector::iterator ConflictPos = std::lower_bound(CmdLineLines.begin(), CmdLineLines.end(), @@ -227,13 +227,11 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, << MacroName; // Show the definition of this macro within the PCH file. - const char *MissingDef = strstr(PCHPredef.data(), Missing.c_str()); - unsigned Offset = MissingDef - PCHPredef.data(); - SourceLocation PCHMissingLoc - = SourceMgr.getLocForStartOfFile(PCHBufferID) - .getFileLocWithOffset(Offset); - Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) - << MacroName; + llvm::StringRef::size_type Offset = PCHPredef.find(Missing); + assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); + SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) + .getFileLocWithOffset(Offset); + Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; ConflictingDefines = true; continue; @@ -250,10 +248,9 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, } // Show the definition of this macro within the PCH file. - const char *MissingDef = strstr(PCHPredef.data(), Missing.c_str()); - unsigned Offset = MissingDef - PCHPredef.data(); - SourceLocation PCHMissingLoc - = SourceMgr.getLocForStartOfFile(PCHBufferID) + llvm::StringRef::size_type Offset = PCHPredef.find(Missing); + assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); + SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) .getFileLocWithOffset(Offset); Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); } @@ -265,13 +262,13 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, // parameters that were not present when building the PCH // file. Extra #defines are okay, so long as the identifiers being // defined were not used within the precompiled header. - std::vector ExtraPredefines; + std::vector ExtraPredefines; std::set_difference(CmdLineLines.begin(), CmdLineLines.end(), PCHLines.begin(), PCHLines.end(), std::back_inserter(ExtraPredefines)); for (unsigned I = 0, N = ExtraPredefines.size(); I != N; ++I) { - const std::string &Extra = ExtraPredefines[I]; - if (!llvm::StringRef(Extra).startswith("#define ")) { + llvm::StringRef &Extra = ExtraPredefines[I]; + if (!Extra.startswith("#define ")) { Reader.Diag(diag::warn_pch_compiler_options_mismatch); return true; } @@ -283,14 +280,12 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, = Extra.find_first_of("( \n\r", StartOfMacroName); assert(EndOfMacroName != std::string::npos && "Couldn't find the end of the macro name"); - std::string MacroName = Extra.substr(StartOfMacroName, - EndOfMacroName - StartOfMacroName); + llvm::StringRef MacroName = Extra.slice(StartOfMacroName, EndOfMacroName); // Check whether this name was used somewhere in the PCH file. If // so, defining it as a macro could change behavior, so we reject // the PCH file. - if (IdentifierInfo *II = Reader.get(MacroName.c_str(), - MacroName.c_str() + MacroName.size())) { + if (IdentifierInfo *II = Reader.get(MacroName)) { Reader.Diag(diag::warn_macro_name_used_in_pch) << II; return true; }