Skip to content

Commit

Permalink
[flang][driver] Rename the accessors/mutators (NFC)
Browse files Browse the repository at this point in the history
As per point 3 in [1]:

```
Accessor member functions are named with the non-public data member's
name, less the trailing underscore.  Mutator member functions are named
set_...
```

Originally we just followed the LLVM's style, which is incompatible with
Flang. This patch renames the accessors and mutators accordingly.

`getDiagnostics` and `GetDiagnostics` are replaced with one accessor:
`diagnostics`. `SetDiagnostics` was neither implemented nor used, so
it's deleted.

[1] https://github.com/llvm/llvm-project/blob/master/flang/docs/C++style.md#naming

Differential Revision: https://reviews.llvm.org/D90300
  • Loading branch information
banach-space committed Nov 2, 2020
1 parent 0df1975 commit 9ffb5b0
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 54 deletions.
25 changes: 8 additions & 17 deletions flang/include/flang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ class CompilerInstance {
/// @name Compiler Invocation
/// {

CompilerInvocation &GetInvocation() {
CompilerInvocation &invocation() {
assert(invocation_ && "Compiler instance has no invocation!");
return *invocation_;
};

/// Replace the current invocation.
void SetInvocation(std::shared_ptr<CompilerInvocation> value);
void set_invocation(std::shared_ptr<CompilerInvocation> value);

/// }
/// @name File manager
/// {

/// Return the current allSources.
Fortran::parser::AllSources &GetAllSources() const { return *allSources_; }
Fortran::parser::AllSources &allSources() const { return *allSources_; }

bool HasAllSources() const { return allSources_ != nullptr; }

Expand All @@ -96,9 +96,9 @@ class CompilerInstance {
return invocation_->GetDiagnosticOpts();
}

FrontendOptions &GetFrontendOpts() { return invocation_->GetFrontendOpts(); }
const FrontendOptions &GetFrontendOpts() const {
return invocation_->GetFrontendOpts();
FrontendOptions &frontendOpts() { return invocation_->frontendOpts(); }
const FrontendOptions &frontendOpts() const {
return invocation_->frontendOpts();
}

/// }
Expand All @@ -108,26 +108,17 @@ class CompilerInstance {
bool HasDiagnostics() const { return diagnostics_ != nullptr; }

/// Get the current diagnostics engine.
clang::DiagnosticsEngine &GetDiagnostics() const {
clang::DiagnosticsEngine &diagnostics() const {
assert(diagnostics_ && "Compiler instance has no diagnostics!");
return *diagnostics_;
}

/// Replace the current diagnostics engine.
void SetDiagnostics(clang::DiagnosticsEngine *value);

clang::DiagnosticConsumer &GetDiagnosticClient() const {
assert(diagnostics_ && diagnostics_->getClient() &&
"Compiler instance has no diagnostic client!");
return *diagnostics_->getClient();
}

/// Get the current diagnostics engine.
clang::DiagnosticsEngine &getDiagnostics() const {
assert(diagnostics_ && "Compiler instance has no diagnostics!");
return *diagnostics_;
}

/// {
/// @name Output Files
/// {
Expand Down Expand Up @@ -192,7 +183,7 @@ class CompilerInstance {
/// }
/// @name Output Stream Methods
/// {
void SetOutputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
void set_outputStream(std::unique_ptr<llvm::raw_pwrite_stream> outStream) {
outputStream_ = std::move(outStream);
}

Expand Down
4 changes: 2 additions & 2 deletions flang/include/flang/Frontend/CompilerInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class CompilerInvocation : public CompilerInvocationBase {
public:
CompilerInvocation() = default;

FrontendOptions &GetFrontendOpts() { return frontendOpts_; }
const FrontendOptions &GetFrontendOpts() const { return frontendOpts_; }
FrontendOptions &frontendOpts() { return frontendOpts_; }
const FrontendOptions &frontendOpts() const { return frontendOpts_; }

/// Create a compiler invocation from a list of input options.
/// \returns true on success.
Expand Down
14 changes: 7 additions & 7 deletions flang/include/flang/Frontend/FrontendAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ class FrontendAction {
/// @name Compiler Instance Access
/// @{

CompilerInstance &GetCompilerInstance() const {
CompilerInstance &instance() const {
assert(instance_ && "Compiler instance not registered!");
return *instance_;
}

void SetCompilerInstance(CompilerInstance *value) { instance_ = value; }
void set_instance(CompilerInstance *value) { instance_ = value; }

/// @}
/// @name Current File Information
/// @{

const FrontendInputFile &GetCurrentInput() const { return currentInput_; }
const FrontendInputFile &currentInput() const { return currentInput_; }

llvm::StringRef GetCurrentFile() const {
assert(!currentInput_.IsEmpty() && "No current file!");
return currentInput_.GetFile();
return currentInput_.file();
}

llvm::StringRef GetCurrentFileOrBufferName() const {
assert(!currentInput_.IsEmpty() && "No current file!");
return currentInput_.IsFile()
? currentInput_.GetFile()
: currentInput_.GetBuffer()->getBufferIdentifier();
? currentInput_.file()
: currentInput_.buffer()->getBufferIdentifier();
}
void SetCurrentInput(const FrontendInputFile &currentInput);
void set_currentInput(const FrontendInputFile &currentInput);

/// @}
/// @name Public Action Interface
Expand Down
6 changes: 3 additions & 3 deletions flang/include/flang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ class FrontendInputFile {
FrontendInputFile(const llvm::MemoryBuffer *buffer, InputKind kind)
: buffer_(buffer), kind_(kind) {}

InputKind GetKind() const { return kind_; }
InputKind kind() const { return kind_; }

bool IsEmpty() const { return file_.empty() && buffer_ == nullptr; }
bool IsFile() const { return !IsBuffer(); }
bool IsBuffer() const { return buffer_ != nullptr; }

llvm::StringRef GetFile() const {
llvm::StringRef file() const {
assert(IsFile());
return file_;
}

const llvm::MemoryBuffer *GetBuffer() const {
const llvm::MemoryBuffer *buffer() const {
assert(IsBuffer() && "Requested buffer_, but it is empty!");
return buffer_;
}
Expand Down
6 changes: 3 additions & 3 deletions flang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CompilerInstance::~CompilerInstance() {
assert(outputFiles_.empty() && "Still output files in flight?");
}

void CompilerInstance::SetInvocation(
void CompilerInstance::set_invocation(
std::shared_ptr<CompilerInvocation> value) {
invocation_ = std::move(value);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ CompilerInstance::CreateDefaultOutputFile(

// Get the path of the output file
std::string outputFilePath =
GetOutputFilePath(GetFrontendOpts().outputFile_, baseName, extension);
GetOutputFilePath(frontendOpts().outputFile_, baseName, extension);

// Create the output file
std::unique_ptr<llvm::raw_pwrite_stream> os =
Expand Down Expand Up @@ -120,7 +120,7 @@ void CompilerInstance::ClearOutputFiles(bool eraseFiles) {
bool CompilerInstance::ExecuteAction(FrontendAction &act) {

// Connect Input to a CompileInstance
for (const FrontendInputFile &fif : GetFrontendOpts().inputs_) {
for (const FrontendInputFile &fif : frontendOpts().inputs_) {
if (act.BeginSourceFile(*this, fif)) {
if (llvm::Error err = act.Execute()) {
consumeError(std::move(err));
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
}

// Parse the frontend args
ParseFrontendArgs(res.GetFrontendOpts(), args, diags);
ParseFrontendArgs(res.frontendOpts(), args, diags);

return success;
}
18 changes: 9 additions & 9 deletions flang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

using namespace Fortran::frontend;

void FrontendAction::SetCurrentInput(const FrontendInputFile &currentInput) {
void FrontendAction::set_currentInput(const FrontendInputFile &currentInput) {
this->currentInput_ = currentInput;
}

// Call this method if BeginSourceFile fails.
// Deallocate compiler instance, input and output descriptors
static void BeginSourceFileCleanUp(FrontendAction &fa, CompilerInstance &ci) {
ci.ClearOutputFiles(/*EraseFiles=*/true);
fa.SetCurrentInput(FrontendInputFile());
fa.SetCompilerInstance(nullptr);
fa.set_currentInput(FrontendInputFile());
fa.set_instance(nullptr);
}

bool FrontendAction::BeginSourceFile(
Expand All @@ -31,8 +31,8 @@ bool FrontendAction::BeginSourceFile(
FrontendInputFile input(realInput);
assert(!instance_ && "Already processing a source file!");
assert(!realInput.IsEmpty() && "Unexpected empty filename!");
SetCurrentInput(realInput);
SetCompilerInstance(&ci);
set_currentInput(realInput);
set_instance(&ci);
if (!ci.HasAllSources()) {
BeginSourceFileCleanUp(*this, ci);
return false;
Expand All @@ -41,7 +41,7 @@ bool FrontendAction::BeginSourceFile(
}

bool FrontendAction::ShouldEraseOutputFiles() {
return GetCompilerInstance().getDiagnostics().hasErrorOccurred();
return instance().diagnostics().hasErrorOccurred();
}

llvm::Error FrontendAction::Execute() {
Expand All @@ -50,12 +50,12 @@ llvm::Error FrontendAction::Execute() {
}

void FrontendAction::EndSourceFile() {
CompilerInstance &ci = GetCompilerInstance();
CompilerInstance &ci = instance();

// Cleanup the output streams, and erase the output files if instructed by the
// FrontendAction.
ci.ClearOutputFiles(/*EraseFiles=*/ShouldEraseOutputFiles());

SetCompilerInstance(nullptr);
SetCurrentInput(FrontendInputFile());
set_instance(nullptr);
set_currentInput(FrontendInputFile());
}
4 changes: 2 additions & 2 deletions flang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ void InputOutputTestAction::ExecuteAction() {
bool binaryMode = true;

// Set/store input file info into CompilerInstance.
CompilerInstance &ci = GetCompilerInstance();
Fortran::parser::AllSources &allSources{ci.GetAllSources()};
CompilerInstance &ci = instance();
Fortran::parser::AllSources &allSources{ci.allSources()};
const Fortran::parser::SourceFile *sf;
sf = allSources.Open(path, error_stream);
llvm::ArrayRef<char> fileContent = sf->content();
Expand Down
6 changes: 3 additions & 3 deletions flang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Fortran::frontend {
static std::unique_ptr<FrontendAction> CreateFrontendBaseAction(
CompilerInstance &ci) {

ActionKind ak = ci.GetFrontendOpts().programAction_;
ActionKind ak = ci.frontendOpts().programAction_;
switch (ak) {
case InputOutputTest:
return std::make_unique<InputOutputTestAction>();
Expand Down Expand Up @@ -52,7 +52,7 @@ std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) {
}
bool ExecuteCompilerInvocation(CompilerInstance *flang) {
// Honor -help.
if (flang->GetFrontendOpts().showHelp_) {
if (flang->frontendOpts().showHelp_) {
clang::driver::getDriverOptTable().PrintHelp(llvm::outs(),
"flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
/*Include=*/clang::driver::options::FC1Option,
Expand All @@ -62,7 +62,7 @@ bool ExecuteCompilerInvocation(CompilerInstance *flang) {
}

// Honor -version.
if (flang->GetFrontendOpts().showVersion_) {
if (flang->frontendOpts().showVersion_) {
llvm::cl::PrintVersionMessage();
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions flang/tools/flang-driver/fc1_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
new clang::DiagnosticOptions();
clang::DiagnosticsEngine diags(diagID, &*diagOpts, diagsBuffer);
bool success =
CompilerInvocation::CreateFromArgs(flang->GetInvocation(), argv, diags);
CompilerInvocation::CreateFromArgs(flang->invocation(), argv, diags);

diagsBuffer->FlushDiagnostics(flang->getDiagnostics());
diagsBuffer->FlushDiagnostics(flang->diagnostics());

if (!success)
return 1;
Expand Down
2 changes: 1 addition & 1 deletion flang/unittests/Frontend/CompilerInstanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TEST(CompilerInstance, SanityCheckForFileManager) {
llvm::raw_string_ostream error_stream{buf};
CompilerInstance compInst;
const Fortran::parser::SourceFile *sf =
compInst.GetAllSources().Open(testFilePath, error_stream);
compInst.allSources().Open(testFilePath, error_stream);

// 3. Verify the content of the input file
// This is just a sanity check to make sure that CompilerInstance is capable
Expand Down
8 changes: 4 additions & 4 deletions flang/unittests/Frontend/InputOutputTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ TEST(FrontendAction, TestInputOutputTestAction) {
CompilerInstance compInst;
compInst.CreateDiagnostics();
auto invocation = std::make_shared<CompilerInvocation>();
invocation->GetFrontendOpts().programAction_ = InputOutputTest;
compInst.SetInvocation(std::move(invocation));
compInst.GetFrontendOpts().inputs_.push_back(
invocation->frontendOpts().programAction_ = InputOutputTest;
compInst.set_invocation(std::move(invocation));
compInst.frontendOpts().inputs_.push_back(
FrontendInputFile(/*File=*/testFilePath, Language::Fortran));

// 3. Set-up the output stream. Using output buffer wrapped as an output
// stream, as opposed to an actual file (or a file descriptor).
llvm::SmallVector<char, 256> outputFileBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
new llvm::raw_svector_ostream(outputFileBuffer));
compInst.SetOutputStream(std::move(outputFileStream));
compInst.set_outputStream(std::move(outputFileStream));

// 4. Run the earlier defined FrontendAction
bool success = ExecuteCompilerInvocation(&compInst);
Expand Down

0 comments on commit 9ffb5b0

Please sign in to comment.