From f0b3de07b6142f4185e12658b7022258666956c8 Mon Sep 17 00:00:00 2001 From: Daniel Paoliello Date: Wed, 19 Jul 2023 12:33:40 -0700 Subject: [PATCH] Fix all formatting and enforce formatting in CI (#170) * Fixed all formatting by running clang-format. * Enforce code is formatted correctly by running clang-format --Werror --dry-run in CI. --- .github/workflows/main.yml | 5 ++ lib/Target/CBackend/CBackend.cpp | 92 +++++++++++++++------------- lib/Target/CBackend/CTargetMachine.h | 19 +++--- tools/llvm-cbe/llvm-cbe.cpp | 12 ++-- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2932eee6..bc7a4628 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -36,6 +36,7 @@ jobs: sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-16 160 sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-16 160 sudo update-alternatives --install /usr/bin/lli lli /usr/bin/lli-16 160 + sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 160 - name: Setup LLVM and GCC (MacOS) if: ${{ runner.os == 'macOS' }} @@ -57,6 +58,10 @@ jobs: python -m pip install --upgrade pip pip install pytest pytest-xdist + - name: Check formatting + if: ${{ runner.os == 'Linux' }} + run: find . -iname '*.h' -o -iname '*.cpp' | xargs clang-format -Werror --dry-run --style=LLVM --verbose + - name: Build run: | mkdir build diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index fd024ab9..dc68dada 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -267,8 +267,7 @@ raw_ostream &CWriter::printTypeString(raw_ostream &Out, Type *Ty, return Out << getFunctionName(cast(Ty)); case Type::FixedVectorTyID: - case Type::ScalableVectorTyID: - { + case Type::ScalableVectorTyID: { TypedefDeclTypes.insert(Ty); FixedVectorType *VTy = cast(Ty); cwriter_assert(VTy->getNumElements() != 0); @@ -545,8 +544,7 @@ CWriter::printTypeName(raw_ostream &Out, Type *Ty, bool isSigned, return Out << getArrayName(cast(Ty)); } case Type::FixedVectorTyID: - case Type::ScalableVectorTyID: - { + case Type::ScalableVectorTyID: { TypedefDeclTypes.insert(Ty); return Out << getVectorName(cast(Ty)); } @@ -708,8 +706,11 @@ bool CWriter::isStandardMain(const FunctionType *FTy) { return false; if (CType.equals("char **") && - (!T->isPointerTy() || !T->getNonOpaquePointerElementType()->isPointerTy() || - !T->getNonOpaquePointerElementType()->getNonOpaquePointerElementType()->isIntegerTy(8))) + (!T->isPointerTy() || + !T->getNonOpaquePointerElementType()->isPointerTy() || + !T->getNonOpaquePointerElementType() + ->getNonOpaquePointerElementType() + ->isIntegerTy(8))) return false; } @@ -725,7 +726,8 @@ CWriter::printFunctionProto(raw_ostream &Out, FunctionType *FTy, AttributeList &PAL = Attrs.first; - if (PAL.hasAttributeAtIndex(AttributeList::FunctionIndex, Attribute::NoReturn)) { + if (PAL.hasAttributeAtIndex(AttributeList::FunctionIndex, + Attribute::NoReturn)) { headerUseNoReturn(); Out << "__noreturn "; } @@ -743,7 +745,8 @@ CWriter::printFunctionProto(raw_ostream &Out, FunctionType *FTy, RetTy = FTy->getReturnType(); else { // If this is a struct-return function, print the struct-return type. - RetTy = cast(FTy->getParamType(0))->getNonOpaquePointerElementType(); + RetTy = cast(FTy->getParamType(0)) + ->getNonOpaquePointerElementType(); } printTypeName( Out, RetTy, @@ -806,9 +809,8 @@ CWriter::printFunctionProto(raw_ostream &Out, FunctionType *FTy, if (shouldFixMain) Out << MainArgs.begin()[Idx].first; else - printTypeName( - Out, ArgTy, - /*isSigned=*/PAL.hasAttributeAtIndex(Idx, Attribute::SExt)); + printTypeName(Out, ArgTy, + /*isSigned=*/PAL.hasAttributeAtIndex(Idx, Attribute::SExt)); PrintedArg = true; if (ArgList) { Out << ' '; @@ -1440,8 +1442,7 @@ void CWriter::printConstant(Constant *CPV, enum OperandContext Context) { break; } case Type::FixedVectorTyID: - case Type::ScalableVectorTyID: - { + case Type::ScalableVectorTyID: { FixedVectorType *VT = cast(CPV->getType()); cwriter_assert(VT->getNumElements() != 0 && !isEmptyType(VT)); if (Context != ContextStatic) { @@ -2285,7 +2286,8 @@ bool CWriter::doInitialization(Module &M) { TAsm = new CBEMCAsmInfo(); MRI = new MCRegisterInfo(); - TCtx = new MCContext(llvm::Triple(TheModule->getTargetTriple()),TAsm, MRI, nullptr); + TCtx = new MCContext(llvm::Triple(TheModule->getTargetTriple()), TAsm, MRI, + nullptr); return false; } @@ -2404,7 +2406,8 @@ void CWriter::generateHeader(Module &M) { // Ignore special globals, such as debug info. if (getGlobalVariableClass(&*I)) continue; - printTypeName(NullOut, I->getType()->getNonOpaquePointerElementType(), false); + printTypeName(NullOut, I->getType()->getNonOpaquePointerElementType(), + false); } printModuleTypes(Out); @@ -2568,8 +2571,9 @@ void CWriter::generateHeader(Module &M) { Out << "\n/* External Alias Declarations */\n"; for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E; ++I) { - cwriter_assert(!I->isDeclaration() && - !isEmptyType(I->getType()->getNonOpaquePointerElementType())); + cwriter_assert( + !I->isDeclaration() && + !isEmptyType(I->getType()->getNonOpaquePointerElementType())); if (I->hasLocalLinkage()) continue; // Internal Global @@ -2632,11 +2636,10 @@ void CWriter::generateHeader(Module &M) { printTypeString(Out, *it, false); Out << "("; if (isa(*it)) - printTypeName( - Out, - VectorType::get(Type::getInt1Ty((*it)->getContext()), - cast(*it)->getElementCount()), - false); + printTypeName(Out, + VectorType::get(Type::getInt1Ty((*it)->getContext()), + cast(*it)->getElementCount()), + false); else Out << "bool"; Out << " condition, "; @@ -2677,7 +2680,8 @@ void CWriter::generateHeader(Module &M) { // } unsigned n, l = NumberOfElements((*it).second); VectorType *RTy = - VectorType::get(Type::getInt1Ty((*it).second->getContext()), l,(*it).second->getElementCount().isScalar()); + VectorType::get(Type::getInt1Ty((*it).second->getContext()), l, + (*it).second->getElementCount().isScalar()); bool isSigned = CmpInst::isSigned((*it).first); Out << "static __forceinline "; printTypeName(Out, RTy, isSigned); @@ -3314,7 +3318,8 @@ void CWriter::declareOneGlobalVariable(GlobalVariable *I) { Type *ElTy = I->getType()->getNonOpaquePointerElementType(); unsigned Alignment = I->getAlignment(); - bool IsOveraligned = Alignment && Alignment > TD->getABITypeAlign(ElTy).value(); + bool IsOveraligned = + Alignment && Alignment > TD->getABITypeAlign(ElTy).value(); if (IsOveraligned) { headerUseAligns(); Out << "__PREFIXALIGN__(" << Alignment << ") "; @@ -3555,8 +3560,8 @@ void CWriter::forwardDeclareStructs(raw_ostream &Out, Type *Ty, if (StructType *ST = dyn_cast(Ty)) { Out << getStructName(ST) << ";\n"; - // Since function declarations come before the definitions of array-wrapper - // structs, it is sometimes necessary to forward-declare those. + // Since function declarations come before the definitions of array-wrapper + // structs, it is sometimes necessary to forward-declare those. } else if (auto *AT = dyn_cast(Ty)) { Out << getArrayName(AT) << ";\n"; } else if (auto *FT = dyn_cast(Ty)) { @@ -3677,8 +3682,8 @@ void CWriter::printFunction(Function &F) { // If this is a struct return function, handle the result with magic. if (isStructReturn) { - Type *StructTy = - cast(F.arg_begin()->getType())->getNonOpaquePointerElementType(); + Type *StructTy = cast(F.arg_begin()->getType()) + ->getNonOpaquePointerElementType(); Out << " "; printTypeName(Out, StructTy, false) << " StructReturn; /* Struct return temporary */\n"; @@ -3694,8 +3699,9 @@ void CWriter::printFunction(Function &F) { for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I) { if (AllocaInst *AI = isDirectAlloca(&*I)) { unsigned Alignment = AI->getAlign().value(); - bool IsOveraligned = Alignment && Alignment > TD->getABITypeAlign( - AI->getAllocatedType()).value(); + bool IsOveraligned = + Alignment && + Alignment > TD->getABITypeAlign(AI->getAllocatedType()).value(); Out << " "; if (IsOveraligned) { headerUseAligns(); @@ -3792,17 +3798,19 @@ void CWriter::printBasicBlock(BasicBlock *BB) { // Output all of the instructions in the basic block... for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E; ++II) { DILocation *Loc = (*II).getDebugLoc(); - if (Loc != nullptr && Loc->getLine() != 0 && LastAnnotatedSourceLine != Loc->getLine()) { + if (Loc != nullptr && Loc->getLine() != 0 && + LastAnnotatedSourceLine != Loc->getLine()) { std::string Directory = Loc->getDirectory().str(); std::replace(Directory.begin(), Directory.end(), '\\', '/'); std::string Filename = Loc->getFilename().str(); std::replace(Filename.begin(), Filename.end(), '\\', '/'); - if (!Directory.empty() && Directory[Directory.size() - 1] != '/' && !Filename.empty() && Filename[0] != '/') + if (!Directory.empty() && Directory[Directory.size() - 1] != '/' && + !Filename.empty() && Filename[0] != '/') Directory.push_back('/'); - Out << "#line " << Loc->getLine() << " \"" << Directory - << Filename << "\"" + Out << "#line " << Loc->getLine() << " \"" << Directory << Filename + << "\"" << "\n"; LastAnnotatedSourceLine = Loc->getLine(); } @@ -4588,13 +4596,13 @@ void CWriter::printIntrinsicDefinition(FunctionType *funT, unsigned Opcode, Out << ";\n"; break; - case Intrinsic::umax: - case Intrinsic::maximum: + case Intrinsic::umax: + case Intrinsic::maximum: Out << " r = a > b ? a : b;\n"; break; - case Intrinsic::umin: - case Intrinsic::minimum: + case Intrinsic::umin: + case Intrinsic::minimum: Out << " r = a < b ? a : b;\n"; break; } @@ -4823,8 +4831,9 @@ void CWriter::visitCallInst(CallInst &I) { if (NeedsCast) { // Ok, just cast the pointer type. Out << "(("; - printTypeName(Out, I.getCalledOperand()->getType()->getNonOpaquePointerElementType(), - false, std::make_pair(PAL, I.getCallingConv())); + printTypeName( + Out, I.getCalledOperand()->getType()->getNonOpaquePointerElementType(), + false, std::make_pair(PAL, I.getCallingConv())); Out << "*)(void*)"; } writeOperand(Callee, ContextCasted); @@ -5327,8 +5336,7 @@ void CWriter::printGEPExpression(Value *Ptr, gep_type_iterator I, Value *Opnd = I.getOperand(); cwriter_assert( - Opnd - ->getType() + Opnd->getType() ->isIntegerTy()); // TODO: indexing a Vector with a Vector is valid, // but we don't support it here diff --git a/lib/Target/CBackend/CTargetMachine.h b/lib/Target/CBackend/CTargetMachine.h index 7c7e8b97..6f197c6c 100644 --- a/lib/Target/CBackend/CTargetMachine.h +++ b/lib/Target/CBackend/CTargetMachine.h @@ -32,14 +32,15 @@ class CTargetSubtargetInfo : public TargetSubtargetInfo { public: #if LLVM_VERSION_MAJOR >= 12 CTargetSubtargetInfo(const TargetMachine &TM, const Triple &TT, StringRef CPU, - StringRef TuneCPU,StringRef FS) + StringRef TuneCPU, StringRef FS) #else CTargetSubtargetInfo(const TargetMachine &TM, const Triple &TT, StringRef CPU, StringRef FS) #endif #if LLVM_VERSION_MAJOR >= 9 #if LLVM_VERSION_MAJOR >= 12 - : TargetSubtargetInfo(TT, CPU,TuneCPU, FS, ArrayRef(), + : TargetSubtargetInfo(TT, CPU, TuneCPU, FS, + ArrayRef(), ArrayRef(), nullptr, nullptr, nullptr, nullptr, nullptr, nullptr), #else @@ -67,11 +68,11 @@ class CTargetMachine : public LLVMTargetMachine { std::optional RM, std::optional CM, #else - llvm:Optional RM, - llvm:Optional CM, + llvm + : Optional RM, llvm + : Optional CM, #endif - CodeGenOpt::Level OL, - bool /*JIT*/) + CodeGenOpt::Level OL, bool /*JIT*/) : LLVMTargetMachine(T, "", TT, CPU, FS, Options, #if LLVM_VERSION_MAJOR >= 16 RM.value_or(Reloc::Static), @@ -82,9 +83,11 @@ class CTargetMachine : public LLVMTargetMachine { #endif OL), #if LLVM_VERSION_MAJOR >= 12 - SubtargetInfo(*this, TT, CPU,"", FS) {} + SubtargetInfo(*this, TT, CPU, "", FS) { + } #else - SubtargetInfo(*this, TT, CPU, FS) {} + SubtargetInfo(*this, TT, CPU, FS) { + } #endif /// Add passes to the specified pass manager to get the specified file diff --git a/tools/llvm-cbe/llvm-cbe.cpp b/tools/llvm-cbe/llvm-cbe.cpp index e7f25996..cc8e45a3 100644 --- a/tools/llvm-cbe/llvm-cbe.cpp +++ b/tools/llvm-cbe/llvm-cbe.cpp @@ -274,8 +274,8 @@ static int compileModule(char **argv, LLVMContext &Context) { #if LLVM_VERSION_MAJOR > 10 auto MAttrs = codegen::getMAttrs(); - bool SkipModule = - codegen::getMCPU() == "help" || (!MAttrs.empty() && MAttrs.front() == "help"); + bool SkipModule = codegen::getMCPU() == "help" || + (!MAttrs.empty() && MAttrs.front() == "help"); #else bool SkipModule = MCPU == "help" || (!MAttrs.empty() && MAttrs.front() == "help"); @@ -304,7 +304,7 @@ static int compileModule(char **argv, LLVMContext &Context) { // Get the target specific parser. std::string Error; // Override MArch - //codegen::getMArch() = "c"; + // codegen::getMArch() = "c"; const std::string MArch = "c"; const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, Error); @@ -377,7 +377,8 @@ static int compileModule(char **argv, LLVMContext &Context) { // OwningPtr std::unique_ptr target(TheTarget->createTargetMachine( #if LLVM_VERSION_MAJOR > 10 - TheTriple.getTriple(), codegen::getMCPU(), FeaturesStr, Options, llvm::codegen::getRelocModel())); + TheTriple.getTriple(), codegen::getMCPU(), FeaturesStr, Options, + llvm::codegen::getRelocModel())); #else TheTriple.getTriple(), MCPU, FeaturesStr, Options, getRelocModel(), getCodeModel(), OLvl)); @@ -436,7 +437,8 @@ static int compileModule(char **argv, LLVMContext &Context) { #else FileType #endif - , NoVerify)) { + , + NoVerify)) { errs() << argv[0] << ": target does not support generation of this" << " file type!\n"; return 1;