[cfe-commits] r169229 - in /cfe/trunk: include/clang/Edit/ include/clang/Lex/ lib/ARCMigrate/ lib/Edit/ lib/Frontend/ lib/Lex/ lib/Serialization/ unittests/Lex/
Argyrios Kyrtzidis
akyrtzi at gmail.com
Mon Dec 3 23:27:05 PST 2012
Author: akirtzidis
Date: Tue Dec 4 01:27:05 2012
New Revision: 169229
URL: http://llvm.org/viewvc/llvm-project?rev=169229&view=rev
Log:
Refactor recording the preprocessor conditional directive regions out of
PreprocessingRecord and into its own class, PPConditionalDirectiveRecord.
Decoupling allows a client to use the functionality of PPConditionalDirectiveRecord
without needing a PreprocessingRecord.
Added:
cfe/trunk/include/clang/Lex/PPConditionalDirectiveRecord.h
cfe/trunk/lib/Lex/PPConditionalDirectiveRecord.cpp
cfe/trunk/unittests/Lex/PPConditionalDirectiveRecordTest.cpp
- copied, changed from r169228, cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp
Removed:
cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp
Modified:
cfe/trunk/include/clang/Edit/Commit.h
cfe/trunk/include/clang/Edit/EditedSource.h
cfe/trunk/include/clang/Lex/PreprocessingRecord.h
cfe/trunk/include/clang/Lex/Preprocessor.h
cfe/trunk/include/clang/Lex/PreprocessorOptions.h
cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
cfe/trunk/lib/Edit/Commit.cpp
cfe/trunk/lib/Frontend/CompilerInstance.cpp
cfe/trunk/lib/Lex/PreprocessingRecord.cpp
cfe/trunk/lib/Lex/Preprocessor.cpp
cfe/trunk/lib/Serialization/ASTReader.cpp
Modified: cfe/trunk/include/clang/Edit/Commit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Edit/Commit.h?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/include/clang/Edit/Commit.h (original)
+++ cfe/trunk/include/clang/Edit/Commit.h Tue Dec 4 01:27:05 2012
@@ -16,7 +16,7 @@
namespace clang {
class LangOptions;
- class PreprocessingRecord;
+ class PPConditionalDirectiveRecord;
namespace edit {
class EditedSource;
@@ -46,7 +46,7 @@
private:
const SourceManager &SourceMgr;
const LangOptions &LangOpts;
- const PreprocessingRecord *PPRec;
+ const PPConditionalDirectiveRecord *PPRec;
EditedSource *Editor;
bool IsCommitable;
@@ -55,7 +55,7 @@
public:
explicit Commit(EditedSource &Editor);
Commit(const SourceManager &SM, const LangOptions &LangOpts,
- const PreprocessingRecord *PPRec = 0)
+ const PPConditionalDirectiveRecord *PPRec = 0)
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), Editor(0),
IsCommitable(true) { }
Modified: cfe/trunk/include/clang/Edit/EditedSource.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Edit/EditedSource.h?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/include/clang/Edit/EditedSource.h (original)
+++ cfe/trunk/include/clang/Edit/EditedSource.h Tue Dec 4 01:27:05 2012
@@ -18,7 +18,7 @@
namespace clang {
class LangOptions;
- class PreprocessingRecord;
+ class PPConditionalDirectiveRecord;
namespace edit {
class Commit;
@@ -27,7 +27,7 @@
class EditedSource {
const SourceManager &SourceMgr;
const LangOptions &LangOpts;
- const PreprocessingRecord *PPRec;
+ const PPConditionalDirectiveRecord *PPRec;
struct FileEdit {
StringRef Text;
@@ -45,13 +45,15 @@
public:
EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
- const PreprocessingRecord *PPRec = 0)
+ const PPConditionalDirectiveRecord *PPRec = 0)
: SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
StrAlloc(/*size=*/512) { }
const SourceManager &getSourceManager() const { return SourceMgr; }
const LangOptions &getLangOpts() const { return LangOpts; }
- const PreprocessingRecord *getPreprocessingRecord() const { return PPRec; }
+ const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
+ return PPRec;
+ }
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
Added: cfe/trunk/include/clang/Lex/PPConditionalDirectiveRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PPConditionalDirectiveRecord.h?rev=169229&view=auto
==============================================================================
--- cfe/trunk/include/clang/Lex/PPConditionalDirectiveRecord.h (added)
+++ cfe/trunk/include/clang/Lex/PPConditionalDirectiveRecord.h Tue Dec 4 01:27:05 2012
@@ -0,0 +1,100 @@
+//===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the PPConditionalDirectiveRecord class, which maintains
+// a record of conditional directive regions.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
+#define LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
+
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Basic/SourceLocation.h"
+#include "llvm/ADT/SmallVector.h"
+#include <vector>
+
+namespace clang {
+
+/// \brief Records preprocessor conditional directive regions and allows
+/// querying in which region source locations belong to.
+class PPConditionalDirectiveRecord : public PPCallbacks {
+ SourceManager &SourceMgr;
+
+ SmallVector<SourceLocation, 6> CondDirectiveStack;
+
+ class CondDirectiveLoc {
+ SourceLocation Loc;
+ SourceLocation RegionLoc;
+
+ public:
+ CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
+ : Loc(Loc), RegionLoc(RegionLoc) {}
+
+ SourceLocation getLoc() const { return Loc; }
+ SourceLocation getRegionLoc() const { return RegionLoc; }
+
+ class Comp {
+ SourceManager &SM;
+ public:
+ explicit Comp(SourceManager &SM) : SM(SM) {}
+ bool operator()(const CondDirectiveLoc &LHS,
+ const CondDirectiveLoc &RHS) {
+ return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
+ }
+ bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
+ return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
+ }
+ bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
+ return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
+ }
+ };
+ };
+
+ typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
+ /// \brief The locations of conditional directives in source order.
+ CondDirectiveLocsTy CondDirectiveLocs;
+
+ void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
+
+public:
+ /// \brief Construct a new preprocessing record.
+ explicit PPConditionalDirectiveRecord(SourceManager &SM);
+
+ size_t getTotalMemory() const;
+
+ SourceManager &getSourceManager() const { return SourceMgr; }
+
+ /// \brief Returns true if the given range intersects with a conditional
+ /// directive. if a \#if/\#endif block is fully contained within the range,
+ /// this function will return false.
+ bool rangeIntersectsConditionalDirective(SourceRange Range) const;
+
+ /// \brief Returns true if the given locations are in different regions,
+ /// separated by conditional directive blocks.
+ bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
+ SourceLocation RHS) const {
+ return findConditionalDirectiveRegionLoc(LHS) !=
+ findConditionalDirectiveRegionLoc(RHS);
+ }
+
+ SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const;
+
+private:
+ virtual void If(SourceLocation Loc, SourceRange ConditionRange);
+ virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
+ SourceLocation IfLoc);
+ virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok);
+ virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok);
+ virtual void Else(SourceLocation Loc, SourceLocation IfLoc);
+ virtual void Endif(SourceLocation Loc, SourceLocation IfLoc);
+};
+
+} // end namespace clang
+
+#endif // LLVM_CLANG_LEX_PPCONDITIONALDIRECTIVERECORD_H
Modified: cfe/trunk/include/clang/Lex/PreprocessingRecord.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessingRecord.h?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessingRecord.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessingRecord.h Tue Dec 4 01:27:05 2012
@@ -303,44 +303,6 @@
/// and are referenced by the iterator using negative indices.
std::vector<PreprocessedEntity *> LoadedPreprocessedEntities;
- bool RecordCondDirectives;
- SmallVector<SourceLocation, 6> CondDirectiveStack;
-
- class CondDirectiveLoc {
- SourceLocation Loc;
- SourceLocation RegionLoc;
-
- public:
- CondDirectiveLoc(SourceLocation Loc, SourceLocation RegionLoc)
- : Loc(Loc), RegionLoc(RegionLoc) {}
-
- SourceLocation getLoc() const { return Loc; }
- SourceLocation getRegionLoc() const { return RegionLoc; }
-
- class Comp {
- SourceManager &SM;
- public:
- explicit Comp(SourceManager &SM) : SM(SM) {}
- bool operator()(const CondDirectiveLoc &LHS,
- const CondDirectiveLoc &RHS) {
- return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS.getLoc());
- }
- bool operator()(const CondDirectiveLoc &LHS, SourceLocation RHS) {
- return SM.isBeforeInTranslationUnit(LHS.getLoc(), RHS);
- }
- bool operator()(SourceLocation LHS, const CondDirectiveLoc &RHS) {
- return SM.isBeforeInTranslationUnit(LHS, RHS.getLoc());
- }
- };
- };
-
- typedef std::vector<CondDirectiveLoc> CondDirectiveLocsTy;
- /// \brief The locations of conditional directives in source order.
- CondDirectiveLocsTy CondDirectiveLocs;
-
- void addCondDirectiveLoc(CondDirectiveLoc DirLoc);
- SourceLocation findCondDirectiveRegionLoc(SourceLocation Loc) const;
-
/// \brief Global (loaded or local) ID for a preprocessed entity.
/// Negative values are used to indicate preprocessed entities
/// loaded from the external source while non-negative values are used to
@@ -398,7 +360,7 @@
public:
/// \brief Construct a new preprocessing record.
- PreprocessingRecord(SourceManager &SM, bool RecordConditionalDirectives);
+ explicit PreprocessingRecord(SourceManager &SM);
/// \brief Allocate memory in the preprocessing record.
void *Allocate(unsigned Size, unsigned Align = 8) {
@@ -582,24 +544,6 @@
/// \brief Add a new preprocessed entity to this record.
PPEntityID addPreprocessedEntity(PreprocessedEntity *Entity);
- /// \brief Returns true if this PreprocessingRecord is keeping track of
- /// conditional directives locations.
- bool isRecordingConditionalDirectives() const {
- return RecordCondDirectives;
- }
-
- /// \brief Returns true if the given range intersects with a conditional
- /// directive. if a \#if/\#endif block is fully contained within the range,
- /// this function will return false.
- bool rangeIntersectsConditionalDirective(SourceRange Range) const;
-
- /// \brief Returns true if the given locations are in different regions,
- /// separated by conditional directive blocks.
- bool areInDifferentConditionalDirectiveRegion(SourceLocation LHS,
- SourceLocation RHS) const {
- return findCondDirectiveRegionLoc(LHS) != findCondDirectiveRegionLoc(RHS);
- }
-
/// \brief Set the external source for preprocessed entities.
void SetExternalSource(ExternalPreprocessingRecordSource &Source);
@@ -626,13 +570,6 @@
StringRef SearchPath,
StringRef RelativePath,
const Module *Imported);
- virtual void If(SourceLocation Loc, SourceRange ConditionRange);
- virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
- SourceLocation IfLoc);
- virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok);
- virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok);
- virtual void Else(SourceLocation Loc, SourceLocation IfLoc);
- virtual void Endif(SourceLocation Loc, SourceLocation IfLoc);
/// \brief Cached result of the last \see getPreprocessedEntitiesInRange
/// query.
Modified: cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/trunk/include/clang/Lex/Preprocessor.h Tue Dec 4 01:27:05 2012
@@ -611,7 +611,7 @@
/// \brief Create a new preprocessing record, which will keep track of
/// all macro expansions, macro definitions, etc.
- void createPreprocessingRecord(bool RecordConditionalDirectives);
+ void createPreprocessingRecord();
/// EnterMainSourceFile - Enter the specified FileID as the main source file,
/// which implicitly adds the builtin defines etc.
Modified: cfe/trunk/include/clang/Lex/PreprocessorOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessorOptions.h?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PreprocessorOptions.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessorOptions.h Tue Dec 4 01:27:05 2012
@@ -53,10 +53,6 @@
unsigned DetailedRecord : 1; /// Whether we should maintain a detailed
/// record of all macro definitions and
/// expansions.
- unsigned DetailedRecordConditionalDirectives : 1; /// Whether in the
- /// preprocessing record we should also keep
- /// track of locations of conditional directives
- /// in non-system files.
/// The implicit PCH included at the start of the translation unit, or empty.
std::string ImplicitPCHInclude;
@@ -179,7 +175,6 @@
public:
PreprocessorOptions() : UsePredefines(true), DetailedRecord(false),
- DetailedRecordConditionalDirectives(false),
DisablePCHValidation(false),
AllowPCHWithCompilerErrors(false),
DumpDeserializedPCHDecls(false),
Modified: cfe/trunk/lib/ARCMigrate/ObjCMT.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/ARCMigrate/ObjCMT.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/ARCMigrate/ObjCMT.cpp (original)
+++ cfe/trunk/lib/ARCMigrate/ObjCMT.cpp Tue Dec 4 01:27:05 2012
@@ -20,6 +20,7 @@
#include "clang/Edit/EditsReceiver.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/PPConditionalDirectiveRecord.h"
#include "clang/Basic/FileManager.h"
#include "llvm/ADT/SmallString.h"
@@ -39,7 +40,7 @@
llvm::OwningPtr<edit::EditedSource> Editor;
FileRemapper &Remapper;
FileManager &FileMgr;
- const PreprocessingRecord *PPRec;
+ const PPConditionalDirectiveRecord *PPRec;
bool IsOutputFile;
ObjCMigrateASTConsumer(StringRef migrateDir,
@@ -47,7 +48,7 @@
bool migrateSubscripting,
FileRemapper &remapper,
FileManager &fileMgr,
- const PreprocessingRecord *PPRec,
+ const PPConditionalDirectiveRecord *PPRec,
bool isOutputFile = false)
: MigrateDir(migrateDir),
MigrateLiterals(migrateLiterals),
@@ -93,6 +94,9 @@
ASTConsumer *ObjCMigrateAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
+ PPConditionalDirectiveRecord *
+ PPRec = new PPConditionalDirectiveRecord(CompInst->getSourceManager());
+ CompInst->getPreprocessor().addPPCallbacks(PPRec);
ASTConsumer *
WrappedConsumer = WrapperFrontendAction::CreateASTConsumer(CI, InFile);
ASTConsumer *MTConsumer = new ObjCMigrateASTConsumer(MigrateDir,
@@ -100,7 +104,7 @@
MigrateSubscripting,
Remapper,
CompInst->getFileManager(),
- CompInst->getPreprocessor().getPreprocessingRecord());
+ PPRec);
ASTConsumer *Consumers[] = { MTConsumer, WrappedConsumer };
return new MultiplexConsumer(Consumers);
}
@@ -110,8 +114,6 @@
/*ignoreIfFilesChanges=*/true);
CompInst = &CI;
CI.getDiagnostics().setIgnoreAllWarnings(true);
- CI.getPreprocessorOpts().DetailedRecord = true;
- CI.getPreprocessorOpts().DetailedRecordConditionalDirectives = true;
return true;
}
@@ -211,18 +213,19 @@
bool MigrateSourceAction::BeginInvocation(CompilerInstance &CI) {
CI.getDiagnostics().setIgnoreAllWarnings(true);
- CI.getPreprocessorOpts().DetailedRecord = true;
- CI.getPreprocessorOpts().DetailedRecordConditionalDirectives = true;
return true;
}
ASTConsumer *MigrateSourceAction::CreateASTConsumer(CompilerInstance &CI,
StringRef InFile) {
+ PPConditionalDirectiveRecord *
+ PPRec = new PPConditionalDirectiveRecord(CI.getSourceManager());
+ CI.getPreprocessor().addPPCallbacks(PPRec);
return new ObjCMigrateASTConsumer(CI.getFrontendOpts().OutputFile,
/*MigrateLiterals=*/true,
/*MigrateSubscripting=*/true,
Remapper,
CI.getFileManager(),
- CI.getPreprocessor().getPreprocessingRecord(),
+ PPRec,
/*isOutputFile=*/true);
}
Modified: cfe/trunk/lib/Edit/Commit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Edit/Commit.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/Edit/Commit.cpp (original)
+++ cfe/trunk/lib/Edit/Commit.cpp Tue Dec 4 01:27:05 2012
@@ -10,7 +10,7 @@
#include "clang/Edit/Commit.h"
#include "clang/Edit/EditedSource.h"
#include "clang/Lex/Lexer.h"
-#include "clang/Lex/PreprocessingRecord.h"
+#include "clang/Lex/PPConditionalDirectiveRecord.h"
#include "clang/Basic/SourceManager.h"
using namespace clang;
@@ -37,7 +37,7 @@
Commit::Commit(EditedSource &Editor)
: SourceMgr(Editor.getSourceManager()), LangOpts(Editor.getLangOpts()),
- PPRec(Editor.getPreprocessingRecord()),
+ PPRec(Editor.getPPCondDirectiveRecord()),
Editor(&Editor), IsCommitable(true) { }
bool Commit::insert(SourceLocation loc, StringRef text,
Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Tue Dec 4 01:27:05 2012
@@ -260,7 +260,7 @@
}
if (PPOpts.DetailedRecord)
- PP->createPreprocessingRecord(PPOpts.DetailedRecordConditionalDirectives);
+ PP->createPreprocessingRecord();
InitializePreprocessor(*PP, PPOpts, getHeaderSearchOpts(), getFrontendOpts());
Added: cfe/trunk/lib/Lex/PPConditionalDirectiveRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPConditionalDirectiveRecord.cpp?rev=169229&view=auto
==============================================================================
--- cfe/trunk/lib/Lex/PPConditionalDirectiveRecord.cpp (added)
+++ cfe/trunk/lib/Lex/PPConditionalDirectiveRecord.cpp Tue Dec 4 01:27:05 2012
@@ -0,0 +1,118 @@
+//===--- PPConditionalDirectiveRecord.h - Preprocessing Directives-*- C++ -*-=//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the PPConditionalDirectiveRecord class, which maintains
+// a record of conditional directive regions.
+//
+//===----------------------------------------------------------------------===//
+#include "clang/Lex/PPConditionalDirectiveRecord.h"
+#include "llvm/Support/Capacity.h"
+
+using namespace clang;
+
+PPConditionalDirectiveRecord::PPConditionalDirectiveRecord(SourceManager &SM)
+ : SourceMgr(SM) {
+ CondDirectiveStack.push_back(SourceLocation());
+}
+
+bool PPConditionalDirectiveRecord::rangeIntersectsConditionalDirective(
+ SourceRange Range) const {
+ if (Range.isInvalid())
+ return false;
+
+ CondDirectiveLocsTy::const_iterator
+ low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
+ Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr));
+ if (low == CondDirectiveLocs.end())
+ return false;
+
+ if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc()))
+ return false;
+
+ CondDirectiveLocsTy::const_iterator
+ upp = std::upper_bound(low, CondDirectiveLocs.end(),
+ Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr));
+ SourceLocation uppRegion;
+ if (upp != CondDirectiveLocs.end())
+ uppRegion = upp->getRegionLoc();
+
+ return low->getRegionLoc() != uppRegion;
+}
+
+SourceLocation PPConditionalDirectiveRecord::findConditionalDirectiveRegionLoc(
+ SourceLocation Loc) const {
+ if (Loc.isInvalid())
+ return SourceLocation();
+ if (CondDirectiveLocs.empty())
+ return SourceLocation();
+
+ if (SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
+ Loc))
+ return CondDirectiveStack.back();
+
+ CondDirectiveLocsTy::const_iterator
+ low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
+ Loc, CondDirectiveLoc::Comp(SourceMgr));
+ assert(low != CondDirectiveLocs.end());
+ return low->getRegionLoc();
+}
+
+void PPConditionalDirectiveRecord::addCondDirectiveLoc(
+ CondDirectiveLoc DirLoc) {
+ // Ignore directives in system headers.
+ if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
+ return;
+
+ assert(CondDirectiveLocs.empty() ||
+ SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
+ DirLoc.getLoc()));
+ CondDirectiveLocs.push_back(DirLoc);
+}
+
+void PPConditionalDirectiveRecord::If(SourceLocation Loc,
+ SourceRange ConditionRange) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ CondDirectiveStack.push_back(Loc);
+}
+
+void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
+ const Token &MacroNameTok) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ CondDirectiveStack.push_back(Loc);
+}
+
+void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
+ const Token &MacroNameTok) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ CondDirectiveStack.push_back(Loc);
+}
+
+void PPConditionalDirectiveRecord::Elif(SourceLocation Loc,
+ SourceRange ConditionRange,
+ SourceLocation IfLoc) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ CondDirectiveStack.back() = Loc;
+}
+
+void PPConditionalDirectiveRecord::Else(SourceLocation Loc,
+ SourceLocation IfLoc) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ CondDirectiveStack.back() = Loc;
+}
+
+void PPConditionalDirectiveRecord::Endif(SourceLocation Loc,
+ SourceLocation IfLoc) {
+ addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
+ assert(!CondDirectiveStack.empty());
+ CondDirectiveStack.pop_back();
+}
+
+size_t PPConditionalDirectiveRecord::getTotalMemory() const {
+ return llvm::capacity_in_bytes(CondDirectiveLocs);
+}
Modified: cfe/trunk/lib/Lex/PreprocessingRecord.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PreprocessingRecord.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PreprocessingRecord.cpp (original)
+++ cfe/trunk/lib/Lex/PreprocessingRecord.cpp Tue Dec 4 01:27:05 2012
@@ -38,14 +38,9 @@
this->FileName = StringRef(Memory, FileName.size());
}
-PreprocessingRecord::PreprocessingRecord(SourceManager &SM,
- bool RecordConditionalDirectives)
+PreprocessingRecord::PreprocessingRecord(SourceManager &SM)
: SourceMgr(SM),
- RecordCondDirectives(RecordConditionalDirectives),
- ExternalSource(0)
-{
- if (RecordCondDirectives)
- CondDirectiveStack.push_back(SourceLocation());
+ ExternalSource(0) {
}
/// \brief Returns a pair of [Begin, End) iterators of preprocessed entities
@@ -438,98 +433,6 @@
addPreprocessedEntity(ID);
}
-bool PreprocessingRecord::rangeIntersectsConditionalDirective(
- SourceRange Range) const {
- if (Range.isInvalid())
- return false;
-
- CondDirectiveLocsTy::const_iterator
- low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
- Range.getBegin(), CondDirectiveLoc::Comp(SourceMgr));
- if (low == CondDirectiveLocs.end())
- return false;
-
- if (SourceMgr.isBeforeInTranslationUnit(Range.getEnd(), low->getLoc()))
- return false;
-
- CondDirectiveLocsTy::const_iterator
- upp = std::upper_bound(low, CondDirectiveLocs.end(),
- Range.getEnd(), CondDirectiveLoc::Comp(SourceMgr));
- SourceLocation uppRegion;
- if (upp != CondDirectiveLocs.end())
- uppRegion = upp->getRegionLoc();
-
- return low->getRegionLoc() != uppRegion;
-}
-
-SourceLocation
-PreprocessingRecord::findCondDirectiveRegionLoc(SourceLocation Loc) const {
- if (Loc.isInvalid())
- return SourceLocation();
-
- CondDirectiveLocsTy::const_iterator
- low = std::lower_bound(CondDirectiveLocs.begin(), CondDirectiveLocs.end(),
- Loc, CondDirectiveLoc::Comp(SourceMgr));
- if (low == CondDirectiveLocs.end())
- return SourceLocation();
- return low->getRegionLoc();
-}
-
-void PreprocessingRecord::addCondDirectiveLoc(CondDirectiveLoc DirLoc) {
- // Ignore directives in system headers.
- if (SourceMgr.isInSystemHeader(DirLoc.getLoc()))
- return;
-
- assert(CondDirectiveLocs.empty() ||
- SourceMgr.isBeforeInTranslationUnit(CondDirectiveLocs.back().getLoc(),
- DirLoc.getLoc()));
- CondDirectiveLocs.push_back(DirLoc);
-}
-
-void PreprocessingRecord::If(SourceLocation Loc, SourceRange ConditionRange) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- CondDirectiveStack.push_back(Loc);
- }
-}
-
-void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- CondDirectiveStack.push_back(Loc);
- }
-}
-
-void PreprocessingRecord::Ifndef(SourceLocation Loc,const Token &MacroNameTok) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- CondDirectiveStack.push_back(Loc);
- }
-}
-
-void PreprocessingRecord::Elif(SourceLocation Loc, SourceRange ConditionRange,
- SourceLocation IfLoc) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- CondDirectiveStack.back() = Loc;
- }
-}
-
-void PreprocessingRecord::Else(SourceLocation Loc, SourceLocation IfLoc) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- CondDirectiveStack.back() = Loc;
- }
-}
-
-void PreprocessingRecord::Endif(SourceLocation Loc, SourceLocation IfLoc) {
- if (RecordCondDirectives) {
- addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
- assert(!CondDirectiveStack.empty());
- CondDirectiveStack.pop_back();
- }
-}
-
size_t PreprocessingRecord::getTotalMemory() const {
return BumpAlloc.getTotalMemory()
+ llvm::capacity_in_bytes(MacroDefinitions)
Modified: cfe/trunk/lib/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Preprocessor.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Preprocessor.cpp (original)
+++ cfe/trunk/lib/Lex/Preprocessor.cpp Tue Dec 4 01:27:05 2012
@@ -765,11 +765,10 @@
CodeCompletionHandler::~CodeCompletionHandler() { }
-void Preprocessor::createPreprocessingRecord(bool RecordConditionalDirectives) {
+void Preprocessor::createPreprocessingRecord() {
if (Record)
return;
- Record = new PreprocessingRecord(getSourceManager(),
- RecordConditionalDirectives);
+ Record = new PreprocessingRecord(getSourceManager());
addPPCallbacks(Record);
}
Modified: cfe/trunk/lib/Serialization/ASTReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReader.cpp?rev=169229&r1=169228&r2=169229&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReader.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReader.cpp Tue Dec 4 01:27:05 2012
@@ -1907,7 +1907,7 @@
= F.PreprocessorDetailCursor.GetCurrentBitNo();
if (!PP.getPreprocessingRecord())
- PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
+ PP.createPreprocessingRecord();
if (!PP.getPreprocessingRecord()->getExternalSource())
PP.getPreprocessingRecord()->SetExternalSource(*this);
break;
@@ -2365,7 +2365,7 @@
unsigned StartingID;
if (!PP.getPreprocessingRecord())
- PP.createPreprocessingRecord(/*RecordConditionalDirectives=*/false);
+ PP.createPreprocessingRecord();
if (!PP.getPreprocessingRecord()->getExternalSource())
PP.getPreprocessingRecord()->SetExternalSource(*this);
StartingID
Copied: cfe/trunk/unittests/Lex/PPConditionalDirectiveRecordTest.cpp (from r169228, cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp)
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Lex/PPConditionalDirectiveRecordTest.cpp?p2=cfe/trunk/unittests/Lex/PPConditionalDirectiveRecordTest.cpp&p1=cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp&r1=169228&r2=169229&rev=169229&view=diff
==============================================================================
--- cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp (original)
+++ cfe/trunk/unittests/Lex/PPConditionalDirectiveRecordTest.cpp Tue Dec 4 01:27:05 2012
@@ -1,4 +1,4 @@
-//===- unittests/Lex/PreprocessingRecordTest.cpp - PreprocessingRecord tests =//
+//===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//
//
// The LLVM Compiler Infrastructure
//
@@ -19,7 +19,7 @@
#include "clang/Lex/HeaderSearchOptions.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
-#include "clang/Lex/PreprocessingRecord.h"
+#include "clang/Lex/PPConditionalDirectiveRecord.h"
#include "llvm/Config/config.h"
#include "gtest/gtest.h"
@@ -30,9 +30,9 @@
namespace {
// The test fixture.
-class PreprocessingRecordTest : public ::testing::Test {
+class PPConditionalDirectiveRecordTest : public ::testing::Test {
protected:
- PreprocessingRecordTest()
+ PPConditionalDirectiveRecordTest()
: FileMgr(FileMgrOpts),
DiagID(new DiagnosticIDs()),
Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
@@ -62,7 +62,7 @@
}
};
-TEST_F(PreprocessingRecordTest, PPRecAPI) {
+TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
const char *source =
"0 1\n"
"#if 1\n"
@@ -93,7 +93,9 @@
/*IILookup =*/ 0,
/*OwnsHeaderSearch =*/false,
/*DelayInitialization =*/ false);
- PP.createPreprocessingRecord(true);
+ PPConditionalDirectiveRecord *
+ PPRec = new PPConditionalDirectiveRecord(SourceMgr);
+ PP.addPPCallbacks(PPRec);
PP.EnterMainSourceFile();
std::vector<Token> toks;
@@ -108,37 +110,36 @@
// Make sure we got the tokens that we expected.
ASSERT_EQ(10U, toks.size());
- PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[0].getLocation(), toks[1].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[0].getLocation(), toks[2].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[3].getLocation(), toks[4].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[1].getLocation(), toks[5].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[2].getLocation(), toks[6].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[2].getLocation(), toks[5].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[0].getLocation(), toks[6].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[2].getLocation(), toks[8].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
+ EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
SourceRange(toks[0].getLocation(), toks[9].getLocation())));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[0].getLocation(), toks[2].getLocation()));
- EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[3].getLocation(), toks[4].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[1].getLocation(), toks[5].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[2].getLocation(), toks[0].getLocation()));
- EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[4].getLocation(), toks[3].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
+ EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
toks[5].getLocation(), toks[1].getLocation()));
}
Removed: cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp?rev=169228&view=auto
==============================================================================
--- cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp (original)
+++ cfe/trunk/unittests/Lex/PreprocessingRecordTest.cpp (removed)
@@ -1,145 +0,0 @@
-//===- unittests/Lex/PreprocessingRecordTest.cpp - PreprocessingRecord tests =//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "clang/Basic/SourceManager.h"
-#include "clang/Basic/FileManager.h"
-#include "clang/Basic/Diagnostic.h"
-#include "clang/Basic/DiagnosticOptions.h"
-#include "clang/Basic/LangOptions.h"
-#include "clang/Basic/TargetOptions.h"
-#include "clang/Basic/TargetInfo.h"
-#include "clang/Lex/ModuleLoader.h"
-#include "clang/Lex/HeaderSearch.h"
-#include "clang/Lex/HeaderSearchOptions.h"
-#include "clang/Lex/Preprocessor.h"
-#include "clang/Lex/PreprocessorOptions.h"
-#include "clang/Lex/PreprocessingRecord.h"
-#include "llvm/Config/config.h"
-
-#include "gtest/gtest.h"
-
-using namespace llvm;
-using namespace clang;
-
-namespace {
-
-// The test fixture.
-class PreprocessingRecordTest : public ::testing::Test {
-protected:
- PreprocessingRecordTest()
- : FileMgr(FileMgrOpts),
- DiagID(new DiagnosticIDs()),
- Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
- SourceMgr(Diags, FileMgr),
- TargetOpts(new TargetOptions)
- {
- TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
- Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
- }
-
- FileSystemOptions FileMgrOpts;
- FileManager FileMgr;
- IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
- DiagnosticsEngine Diags;
- SourceManager SourceMgr;
- LangOptions LangOpts;
- IntrusiveRefCntPtr<TargetOptions> TargetOpts;
- IntrusiveRefCntPtr<TargetInfo> Target;
-};
-
-class VoidModuleLoader : public ModuleLoader {
- virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
- ModuleIdPath Path,
- Module::NameVisibilityKind Visibility,
- bool IsInclusionDirective) {
- return ModuleLoadResult();
- }
-};
-
-TEST_F(PreprocessingRecordTest, PPRecAPI) {
- const char *source =
- "0 1\n"
- "#if 1\n"
- "2\n"
- "#ifndef BB\n"
- "3 4\n"
- "#else\n"
- "#endif\n"
- "5\n"
- "#endif\n"
- "6\n"
- "#if 1\n"
- "7\n"
- "#if 1\n"
- "#endif\n"
- "8\n"
- "#endif\n"
- "9\n";
-
- MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
- SourceMgr.createMainFileIDForMemBuffer(buf);
-
- VoidModuleLoader ModLoader;
- HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
- Target.getPtr());
- Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts,Target.getPtr(),
- SourceMgr, HeaderInfo, ModLoader,
- /*IILookup =*/ 0,
- /*OwnsHeaderSearch =*/false,
- /*DelayInitialization =*/ false);
- PP.createPreprocessingRecord(true);
- PP.EnterMainSourceFile();
-
- std::vector<Token> toks;
- while (1) {
- Token tok;
- PP.Lex(tok);
- if (tok.is(tok::eof))
- break;
- toks.push_back(tok);
- }
-
- // Make sure we got the tokens that we expected.
- ASSERT_EQ(10U, toks.size());
-
- PreprocessingRecord &PPRec = *PP.getPreprocessingRecord();
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[0].getLocation(), toks[1].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[0].getLocation(), toks[2].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[3].getLocation(), toks[4].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[1].getLocation(), toks[5].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[2].getLocation(), toks[6].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[2].getLocation(), toks[5].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[0].getLocation(), toks[6].getLocation())));
- EXPECT_TRUE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[2].getLocation(), toks[8].getLocation())));
- EXPECT_FALSE(PPRec.rangeIntersectsConditionalDirective(
- SourceRange(toks[0].getLocation(), toks[9].getLocation())));
-
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[0].getLocation(), toks[2].getLocation()));
- EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[3].getLocation(), toks[4].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[1].getLocation(), toks[5].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[2].getLocation(), toks[0].getLocation()));
- EXPECT_FALSE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[4].getLocation(), toks[3].getLocation()));
- EXPECT_TRUE(PPRec.areInDifferentConditionalDirectiveRegion(
- toks[5].getLocation(), toks[1].getLocation()));
-}
-
-} // anonymous namespace
More information about the cfe-commits
mailing list