2013-09-05 01:35:07 +08:00
|
|
|
//===- clang-modernize/TransformTest.cpp - Transform unit tests -----------===//
|
2013-07-11 23:54:06 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-04-06 03:17:36 +08:00
|
|
|
#include "gtest/gtest.h"
|
2013-07-12 22:36:20 +08:00
|
|
|
#include "Core/FileOverrides.h"
|
2013-04-06 03:17:36 +08:00
|
|
|
#include "Core/Transform.h"
|
2013-05-31 01:48:11 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/DeclGroup.h"
|
2013-06-18 23:44:58 +08:00
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
2013-06-26 23:48:36 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2013-05-31 01:48:11 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
2013-06-18 23:44:58 +08:00
|
|
|
using namespace ast_matchers;
|
2013-04-06 03:17:36 +08:00
|
|
|
|
|
|
|
class DummyTransform : public Transform {
|
|
|
|
public:
|
2013-06-07 04:31:52 +08:00
|
|
|
DummyTransform(llvm::StringRef Name, const TransformOptions &Options)
|
|
|
|
: Transform(Name, Options) {}
|
2013-04-06 03:17:36 +08:00
|
|
|
|
2013-09-03 21:16:02 +08:00
|
|
|
virtual int apply(const FileOverrides &,
|
2013-05-31 01:48:11 +08:00
|
|
|
const tooling::CompilationDatabase &,
|
2013-06-18 23:31:01 +08:00
|
|
|
const std::vector<std::string> &) { return 0; }
|
2013-04-06 03:17:36 +08:00
|
|
|
|
|
|
|
void setAcceptedChanges(unsigned Changes) {
|
|
|
|
Transform::setAcceptedChanges(Changes);
|
|
|
|
}
|
|
|
|
void setRejectedChanges(unsigned Changes) {
|
|
|
|
Transform::setRejectedChanges(Changes);
|
|
|
|
}
|
|
|
|
void setDeferredChanges(unsigned Changes) {
|
|
|
|
Transform::setDeferredChanges(Changes);
|
|
|
|
}
|
2013-06-18 02:18:15 +08:00
|
|
|
|
2013-06-18 23:31:01 +08:00
|
|
|
void setOverrides(FileOverrides &Overrides) {
|
2013-06-18 02:18:15 +08:00
|
|
|
Transform::setOverrides(Overrides);
|
|
|
|
}
|
|
|
|
|
2013-04-06 03:17:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Transform, Interface) {
|
2013-06-07 04:31:52 +08:00
|
|
|
TransformOptions Options;
|
|
|
|
DummyTransform T("my_transform", Options);
|
|
|
|
|
2013-04-06 03:17:36 +08:00
|
|
|
ASSERT_EQ("my_transform", T.getName());
|
|
|
|
ASSERT_EQ(0u, T.getAcceptedChanges());
|
|
|
|
ASSERT_EQ(0u, T.getRejectedChanges());
|
|
|
|
ASSERT_EQ(0u, T.getDeferredChanges());
|
|
|
|
ASSERT_FALSE(T.getChangesMade());
|
|
|
|
ASSERT_FALSE(T.getChangesNotMade());
|
|
|
|
|
|
|
|
T.setAcceptedChanges(1);
|
|
|
|
ASSERT_TRUE(T.getChangesMade());
|
|
|
|
|
|
|
|
T.setDeferredChanges(1);
|
|
|
|
ASSERT_TRUE(T.getChangesNotMade());
|
|
|
|
|
|
|
|
T.setRejectedChanges(1);
|
|
|
|
ASSERT_TRUE(T.getChangesNotMade());
|
|
|
|
|
|
|
|
T.Reset();
|
|
|
|
ASSERT_EQ(0u, T.getAcceptedChanges());
|
|
|
|
ASSERT_EQ(0u, T.getRejectedChanges());
|
|
|
|
ASSERT_EQ(0u, T.getDeferredChanges());
|
|
|
|
|
|
|
|
T.setRejectedChanges(1);
|
|
|
|
ASSERT_TRUE(T.getChangesNotMade());
|
|
|
|
}
|
2013-05-31 01:48:11 +08:00
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
class TimePassingASTConsumer : public ASTConsumer {
|
2013-05-31 01:48:11 +08:00
|
|
|
public:
|
2013-06-18 02:18:15 +08:00
|
|
|
TimePassingASTConsumer(bool *Called) : Called(Called) {}
|
2013-05-31 01:48:11 +08:00
|
|
|
|
|
|
|
virtual bool HandleTopLevelDecl(DeclGroupRef DeclGroup) {
|
|
|
|
llvm::sys::TimeValue UserStart;
|
|
|
|
llvm::sys::TimeValue SystemStart;
|
|
|
|
llvm::sys::TimeValue UserNow;
|
|
|
|
llvm::sys::TimeValue SystemNow;
|
|
|
|
llvm::sys::TimeValue Wall;
|
|
|
|
|
|
|
|
// Busy-wait until the user/system time combined is more than 1ms
|
|
|
|
llvm::sys::TimeValue OneMS(0, 1000000);
|
|
|
|
llvm::sys::Process::GetTimeUsage(Wall, UserStart, SystemStart);
|
|
|
|
do {
|
|
|
|
llvm::sys::Process::GetTimeUsage(Wall, UserNow, SystemNow);
|
|
|
|
} while (UserNow - UserStart + SystemNow - SystemStart < OneMS);
|
|
|
|
*Called = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool *Called;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ConsumerFactory {
|
|
|
|
ASTConsumer *newASTConsumer() {
|
2013-06-18 02:18:15 +08:00
|
|
|
return new TimePassingASTConsumer(&Called);
|
2013-05-31 01:48:11 +08:00
|
|
|
}
|
|
|
|
bool Called;
|
|
|
|
};
|
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
struct CallbackForwarder : public clang::tooling::SourceFileCallbacks {
|
|
|
|
CallbackForwarder(Transform &Callee) : Callee(Callee) {}
|
|
|
|
|
|
|
|
virtual bool handleBeginSource(CompilerInstance &CI, StringRef Filename) {
|
|
|
|
return Callee.handleBeginSource(CI, Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void handleEndSource() {
|
|
|
|
Callee.handleEndSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform &Callee;
|
|
|
|
};
|
|
|
|
|
2013-05-31 01:48:11 +08:00
|
|
|
TEST(Transform, Timings) {
|
2013-06-07 04:31:52 +08:00
|
|
|
TransformOptions Options;
|
|
|
|
Options.EnableTiming = true;
|
|
|
|
DummyTransform T("timing_transform", Options);
|
2013-05-31 01:48:11 +08:00
|
|
|
|
|
|
|
// All the path stuff is to make the test work independently of OS.
|
|
|
|
|
|
|
|
// The directory used is not important since the path gets mapped to a virtual
|
|
|
|
// file anyway. What is important is that we have an absolute path with which
|
|
|
|
// to use with mapVirtualFile().
|
2013-06-26 23:48:36 +08:00
|
|
|
SmallString<128> CurrentDir;
|
|
|
|
llvm::error_code EC = llvm::sys::fs::current_path(CurrentDir);
|
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
SmallString<128> FileA = CurrentDir;
|
|
|
|
llvm::sys::path::append(FileA, "a.cc");
|
|
|
|
|
|
|
|
SmallString<128> FileB = CurrentDir;
|
|
|
|
llvm::sys::path::append(FileB, "b.cc");
|
|
|
|
|
|
|
|
tooling::FixedCompilationDatabase Compilations(CurrentDir.str(),
|
|
|
|
std::vector<std::string>());
|
2013-05-31 01:48:11 +08:00
|
|
|
std::vector<std::string> Sources;
|
2013-06-26 23:48:36 +08:00
|
|
|
Sources.push_back(FileA.str());
|
|
|
|
Sources.push_back(FileB.str());
|
2013-05-31 01:48:11 +08:00
|
|
|
tooling::ClangTool Tool(Compilations, Sources);
|
|
|
|
|
2013-06-26 23:48:36 +08:00
|
|
|
Tool.mapVirtualFile(FileA, "void a() {}");
|
|
|
|
Tool.mapVirtualFile(FileB, "void b() {}");
|
2013-05-31 01:48:11 +08:00
|
|
|
|
2013-06-18 02:18:15 +08:00
|
|
|
// Factory to create TimePassingASTConsumer for each source file the tool
|
|
|
|
// runs on.
|
2013-05-31 01:48:11 +08:00
|
|
|
ConsumerFactory Factory;
|
2013-06-18 02:18:15 +08:00
|
|
|
|
|
|
|
// We don't care about any of Transform's functionality except to get it to
|
|
|
|
// record timings. For that, we need to forward handleBeginSource() and
|
|
|
|
// handleEndSource() calls to it.
|
|
|
|
CallbackForwarder Callbacks(T);
|
|
|
|
|
|
|
|
// Transform's handle* functions require FileOverrides to be set, even if
|
|
|
|
// there aren't any.
|
2013-09-03 21:16:02 +08:00
|
|
|
FileOverrides Overrides;
|
2013-06-18 02:18:15 +08:00
|
|
|
T.setOverrides(Overrides);
|
|
|
|
|
|
|
|
Tool.run(clang::tooling::newFrontendActionFactory(&Factory, &Callbacks));
|
2013-05-31 01:48:11 +08:00
|
|
|
|
|
|
|
EXPECT_TRUE(Factory.Called);
|
|
|
|
Transform::TimingVec::const_iterator I = T.timing_begin();
|
|
|
|
EXPECT_GT(I->second.getProcessTime(), 0.0);
|
|
|
|
|
|
|
|
// The success of the test shouldn't depend on the order of iteration through
|
|
|
|
// timers.
|
2013-06-26 23:48:36 +08:00
|
|
|
StringRef FirstFile = I->first;
|
2013-06-15 05:44:06 +08:00
|
|
|
if (FileA == FirstFile) {
|
2013-05-31 01:48:11 +08:00
|
|
|
++I;
|
2013-06-26 23:48:36 +08:00
|
|
|
EXPECT_EQ(FileB, I->first);
|
2013-05-31 01:48:11 +08:00
|
|
|
EXPECT_GT(I->second.getProcessTime(), 0.0);
|
2013-06-15 05:44:06 +08:00
|
|
|
} else if (FileB == FirstFile) {
|
2013-05-31 01:48:11 +08:00
|
|
|
++I;
|
2013-06-26 23:48:36 +08:00
|
|
|
EXPECT_EQ(FileA, I->first);
|
2013-05-31 01:48:11 +08:00
|
|
|
EXPECT_GT(I->second.getProcessTime(), 0.0);
|
|
|
|
} else {
|
|
|
|
FAIL() << "Unexpected file name " << I->first << " in timing data.";
|
|
|
|
}
|
|
|
|
++I;
|
|
|
|
EXPECT_EQ(T.timing_end(), I);
|
|
|
|
}
|
2013-06-18 23:44:58 +08:00
|
|
|
|
|
|
|
class ModifiableCallback
|
|
|
|
: public clang::ast_matchers::MatchFinder::MatchCallback {
|
|
|
|
public:
|
2013-09-06 22:23:56 +08:00
|
|
|
ModifiableCallback(const Transform &Owner)
|
|
|
|
: Owner(Owner) {}
|
2013-06-18 23:44:58 +08:00
|
|
|
|
|
|
|
virtual void
|
|
|
|
run(const clang::ast_matchers::MatchFinder::MatchResult &Result) {
|
|
|
|
const VarDecl *Decl = Result.Nodes.getNodeAs<VarDecl>("decl");
|
|
|
|
ASSERT_TRUE(Decl != 0);
|
|
|
|
|
|
|
|
const SourceManager &SM = *Result.SourceManager;
|
|
|
|
|
|
|
|
// Decl 'a' comes from the main source file. This test should always pass.
|
|
|
|
if (Decl->getName().equals("a"))
|
|
|
|
EXPECT_TRUE(Owner.isFileModifiable(SM, Decl->getLocStart()));
|
|
|
|
|
|
|
|
// Decl 'c' comes from an excluded header. This test should never pass.
|
|
|
|
else if (Decl->getName().equals("c"))
|
|
|
|
EXPECT_FALSE(Owner.isFileModifiable(SM, Decl->getLocStart()));
|
|
|
|
|
2013-09-06 22:23:56 +08:00
|
|
|
// Decl 'b' comes from an included header.
|
2013-06-18 23:44:58 +08:00
|
|
|
else if (Decl->getName().equals("b"))
|
2013-09-06 22:23:56 +08:00
|
|
|
EXPECT_TRUE(Owner.isFileModifiable(SM, Decl->getLocStart()));
|
2013-06-18 23:44:58 +08:00
|
|
|
|
|
|
|
// Make sure edge cases are handled gracefully (they should never be
|
|
|
|
// allowed).
|
|
|
|
SourceLocation DummyLoc;
|
|
|
|
EXPECT_FALSE(Owner.isFileModifiable(SM, DummyLoc));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Transform &Owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Transform, isFileModifiable) {
|
|
|
|
TransformOptions Options;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// SETUP
|
|
|
|
///
|
|
|
|
/// To test Transform::isFileModifiable() we need a SourceManager primed with
|
|
|
|
/// actual files and SourceLocations to test. Easiest way to accomplish this
|
|
|
|
/// is to use Tooling classes.
|
|
|
|
///
|
|
|
|
/// 1) Simulate a source file that includes two headers, one that is allowed
|
|
|
|
/// to be modified and the other that is not allowed. Each of the three
|
|
|
|
/// files involved will declare a single variable with a different name.
|
|
|
|
/// 2) A matcher is created to find VarDecls.
|
|
|
|
/// 3) A MatchFinder callback calls Transform::isFileModifiable() with the
|
|
|
|
/// SourceLocations of found VarDecls and thus tests the function.
|
|
|
|
///
|
|
|
|
|
|
|
|
// All the path stuff is to make the test work independently of OS.
|
|
|
|
|
|
|
|
// The directory used is not important since the path gets mapped to a virtual
|
|
|
|
// file anyway. What is important is that we have an absolute path with which
|
|
|
|
// to use with mapVirtualFile().
|
2013-06-26 23:48:36 +08:00
|
|
|
SmallString<128> CurrentDir;
|
|
|
|
llvm::error_code EC = llvm::sys::fs::current_path(CurrentDir);
|
|
|
|
assert(!EC);
|
|
|
|
(void)EC;
|
|
|
|
|
|
|
|
SmallString<128> SourceFile = CurrentDir;
|
|
|
|
llvm::sys::path::append(SourceFile, "a.cc");
|
|
|
|
|
|
|
|
SmallString<128> HeaderFile = CurrentDir;
|
|
|
|
llvm::sys::path::append(HeaderFile, "a.h");
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-06-26 23:48:36 +08:00
|
|
|
SmallString<128> HeaderBFile = CurrentDir;
|
|
|
|
llvm::sys::path::append(HeaderBFile, "temp");
|
|
|
|
llvm::sys::path::append(HeaderBFile, "b.h");
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-06-26 23:48:36 +08:00
|
|
|
StringRef ExcludeDir = llvm::sys::path::parent_path(HeaderBFile);
|
2013-06-18 23:44:58 +08:00
|
|
|
|
|
|
|
IncludeExcludeInfo IncInfo;
|
2013-09-13 04:10:59 +08:00
|
|
|
Options.ModifiableFiles.readListFromString(CurrentDir, ExcludeDir);
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-06-26 23:48:36 +08:00
|
|
|
tooling::FixedCompilationDatabase Compilations(CurrentDir.str(),
|
|
|
|
std::vector<std::string>());
|
2013-06-18 23:44:58 +08:00
|
|
|
std::vector<std::string> Sources;
|
2013-06-26 23:48:36 +08:00
|
|
|
Sources.push_back(SourceFile.str());
|
2013-06-18 23:44:58 +08:00
|
|
|
tooling::ClangTool Tool(Compilations, Sources);
|
|
|
|
|
2013-06-26 23:48:36 +08:00
|
|
|
Tool.mapVirtualFile(SourceFile,
|
2013-06-18 23:44:58 +08:00
|
|
|
"#include \"a.h\"\n"
|
|
|
|
"#include \"temp/b.h\"\n"
|
|
|
|
"int a;");
|
2013-06-26 23:48:36 +08:00
|
|
|
Tool.mapVirtualFile(HeaderFile, "int b;");
|
|
|
|
Tool.mapVirtualFile(HeaderBFile, "int c;");
|
2013-06-18 23:44:58 +08:00
|
|
|
|
2013-09-06 22:23:56 +08:00
|
|
|
DummyTransform T("dummy", Options);
|
|
|
|
MatchFinder Finder;
|
|
|
|
Finder.addMatcher(varDecl().bind("decl"), new ModifiableCallback(T));
|
|
|
|
Tool.run(tooling::newFrontendActionFactory(&Finder));
|
2013-06-18 23:44:58 +08:00
|
|
|
}
|
2013-07-29 23:58:47 +08:00
|
|
|
|
|
|
|
TEST(VersionTest, Interface) {
|
|
|
|
Version V;
|
|
|
|
|
|
|
|
ASSERT_TRUE(V.isNull());
|
|
|
|
ASSERT_TRUE(Version(1) < Version(1, 1));
|
|
|
|
ASSERT_TRUE(Version(1) < Version(2));
|
|
|
|
ASSERT_TRUE(Version(1, 1) < Version(2));
|
|
|
|
ASSERT_TRUE(Version(1, 1) == Version(1, 1));
|
|
|
|
ASSERT_EQ(Version(1).getMajor(), unsigned(1));
|
|
|
|
ASSERT_EQ(Version(1).getMinor(), unsigned(0));
|
|
|
|
ASSERT_EQ(Version(1, 2).getMinor(), unsigned(2));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(VersionTest, getFromString) {
|
|
|
|
ASSERT_EQ(Version(1), Version::getFromString("1"));
|
|
|
|
ASSERT_EQ(Version(1, 2), Version::getFromString("1.2"));
|
|
|
|
ASSERT_TRUE(Version::getFromString("foo").isNull());
|
|
|
|
ASSERT_TRUE(Version::getFromString("1bar").isNull());
|
|
|
|
// elements after major.minor are ignored
|
|
|
|
ASSERT_EQ(Version(1, 2), Version::getFromString("1.2.3"));
|
|
|
|
}
|