Ejemplo n.º 1
0
        public static string ParseIncludes(ModuleDecl module, BuiltIns builtIns, IList <string> excludeFiles, Errors errs)
        {
            SortedSet <Include> includes = new SortedSet <Include>(new IncludeComparer());
            DependencyMap       dmap     = new DependencyMap();

            foreach (string fileName in excludeFiles)
            {
                includes.Add(new Include(null, null, fileName));
            }
            dmap.AddIncludes(includes);
            bool newlyIncluded;

            do
            {
                newlyIncluded = false;

                List <Include> newFilesToInclude = new List <Include>();
                dmap.AddIncludes(((LiteralModuleDecl)module).ModuleDef.Includes);
                foreach (Include include in ((LiteralModuleDecl)module).ModuleDef.Includes)
                {
                    bool isNew = includes.Add(include);
                    if (isNew)
                    {
                        newlyIncluded = true;
                        newFilesToInclude.Add(include);
                    }
                }

                foreach (Include include in newFilesToInclude)
                {
                    DafnyFile file;
                    try { file = new DafnyFile(include.includedFilename); }
                    catch (IllegalDafnyFile) {
                        return(String.Format("Include of file \"{0}\" failed.", include.includedFilename));
                    }
                    string ret = ParseFile(file, include, module, builtIns, errs, false);
                    if (ret != null)
                    {
                        return(ret);
                    }
                }
            } while (newlyIncluded);


            if (DafnyOptions.O.PrintIncludesMode != DafnyOptions.IncludesModes.None)
            {
                dmap.PrintMap();
            }

            return(null); // Success
        }
Ejemplo n.º 2
0
        public static string Parse(IList <DafnyFile> files, string programName, ErrorReporter reporter, out Program program)
        {
            Contract.Requires(programName != null);
            Contract.Requires(files != null);
            program = null;

            ModuleDecl module = new LiteralModuleDecl(new DefaultModuleDecl(), null);

            BuiltIns builtIns = new BuiltIns();

            foreach (DafnyFile dafnyFile in files)
            {
                Contract.Assert(dafnyFile != null);
                if (Bpl.CommandLineOptions.Clo.XmlSink != null && Bpl.CommandLineOptions.Clo.XmlSink.IsOpen)
                {
                    Bpl.CommandLineOptions.Clo.XmlSink.WriteFileFragment(dafnyFile.FilePath);
                }
                if (Bpl.CommandLineOptions.Clo.Trace)
                {
                    Console.WriteLine("Parsing " + dafnyFile.FilePath);
                }

                string err = ParseFile(dafnyFile, null, module, builtIns, new Errors(reporter));
                if (err != null)
                {
                    return(err);
                }
            }

            if (!(DafnyOptions.O.DisallowIncludes || DafnyOptions.O.PrintIncludesMode == DafnyOptions.IncludesModes.Immediate))
            {
                string errString = ParseIncludes(module, builtIns, DafnyFile.fileNames(files), new Errors(reporter));
                if (errString != null)
                {
                    return(errString);
                }
            }

            if (DafnyOptions.O.PrintIncludesMode == DafnyOptions.IncludesModes.Immediate)
            {
                DependencyMap dmap = new DependencyMap();
                dmap.AddIncludes(((LiteralModuleDecl)module).ModuleDef.Includes);
                dmap.PrintMap();
            }

            program = new Program(programName, module, builtIns, reporter);

            MaybePrintProgram(program, DafnyOptions.O.DafnyPrintFile, false);

            return(null); // success
        }
Ejemplo n.º 3
0
        private static string ParseFile(DafnyFile dafnyFile, Bpl.IToken tok, ModuleDecl module, BuiltIns builtIns, Errors errs, bool verifyThisFile = true)
        {
            var fn = DafnyOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(dafnyFile.FilePath) : dafnyFile.FilePath;

            try {
                int errorCount = Dafny.Parser.Parse(dafnyFile.SourceFileName, module, builtIns, errs, verifyThisFile);
                if (errorCount != 0)
                {
                    return(string.Format("{0} parse errors detected in {1}", errorCount, fn));
                }
            } catch (IOException e) {
                errs.SemErr(tok, "Unable to open included file");
                return(string.Format("Error opening file \"{0}\": {1}", fn, e.Message));
            }
            return(null); // Success
        }
Ejemplo n.º 4
0
        static ExitValue ProcessFiles(IList <DafnyFile /*!*/> /*!*/ dafnyFiles, ReadOnlyCollection <string> otherFileNames,
                                      ErrorReporter reporter, bool lookForSnapshots = true, string programId = null)
        {
            Contract.Requires(cce.NonNullElements(dafnyFiles));
            var dafnyFileNames = DafnyFile.fileNames(dafnyFiles);

            ExitValue exitValue = ExitValue.VERIFIED;

            if (CommandLineOptions.Clo.VerifySeparately && 1 < dafnyFiles.Count)
            {
                foreach (var f in dafnyFiles)
                {
                    Console.WriteLine();
                    Console.WriteLine("-------------------- {0} --------------------", f);
                    var ev = ProcessFiles(new List <DafnyFile> {
                        f
                    }, new List <string>().AsReadOnly(), reporter, lookForSnapshots, f.FilePath);
                    if (exitValue != ev && ev != ExitValue.VERIFIED)
                    {
                        exitValue = ev;
                    }
                }
                return(exitValue);
            }

            if (0 <= CommandLineOptions.Clo.VerifySnapshots && lookForSnapshots)
            {
                var snapshotsByVersion = ExecutionEngine.LookForSnapshots(dafnyFileNames);
                foreach (var s in snapshotsByVersion)
                {
                    var snapshots = new List <DafnyFile>();
                    foreach (var f in s)
                    {
                        snapshots.Add(new DafnyFile(f));
                    }
                    var ev = ProcessFiles(snapshots, new List <string>().AsReadOnly(), reporter, false, programId);
                    if (exitValue != ev && ev != ExitValue.VERIFIED)
                    {
                        exitValue = ev;
                    }
                }
                return(exitValue);
            }

            Dafny.Program dafnyProgram;
            string        programName = dafnyFileNames.Count == 1 ? dafnyFileNames[0] : "the program";
            string        err         = Dafny.Main.ParseCheck(dafnyFiles, programName, reporter, out dafnyProgram);

            if (err != null)
            {
                exitValue = ExitValue.DAFNY_ERROR;
                ExecutionEngine.printer.ErrorWriteLine(Console.Out, err);
            }
            else if (dafnyProgram != null && !CommandLineOptions.Clo.NoResolve && !CommandLineOptions.Clo.NoTypecheck &&
                     DafnyOptions.O.DafnyVerify)
            {
                var boogiePrograms = Translate(dafnyProgram);

                Dictionary <string, PipelineStatistics> statss;
                PipelineOutcome oc;
                string          baseName = cce.NonNull(Path.GetFileName(dafnyFileNames[dafnyFileNames.Count - 1]));
                var             verified = Boogie(baseName, boogiePrograms, programId, out statss, out oc);
                var             compiled = Compile(dafnyFileNames[0], otherFileNames, dafnyProgram, oc, statss, verified);
                exitValue = verified && compiled ? ExitValue.VERIFIED : !verified ? ExitValue.NOT_VERIFIED : ExitValue.COMPILE_ERROR;
            }

            if (err == null && dafnyProgram != null && DafnyOptions.O.PrintStats)
            {
                Util.PrintStats(dafnyProgram);
            }
            if (err == null && dafnyProgram != null && DafnyOptions.O.PrintFunctionCallGraph)
            {
                Util.PrintFunctionCallGraph(dafnyProgram);
            }
            return(exitValue);
        }
Ejemplo n.º 5
0
        public static CommandLineArgumentsResult ProcessCommandLineArguments(string[] args, out List <DafnyFile> dafnyFiles, out List <string> otherFiles)
        {
            dafnyFiles = new List <DafnyFile>();
            otherFiles = new List <string>();

            CommandLineOptions.Clo.RunningBoogieFromCommandLine = true;
            try {
                if (!CommandLineOptions.Clo.Parse(args))
                {
                    return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                }
            } catch (ProverException pe) {
                ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** ProverException: {0}", pe.Message);
                return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
            }

            // If requested, print version number, help, attribute help, etc. and exit.
            if (DafnyOptions.O.ProcessInfoFlags())
            {
                return(CommandLineArgumentsResult.OK_EXIT_EARLY);
            }

            if (DafnyOptions.O.UseStdin)
            {
                dafnyFiles.Add(new DafnyFile("<stdin>", true));
            }
            else if (CommandLineOptions.Clo.Files.Count == 0)
            {
                ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: No input files were specified.");
                return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
            }
            if (CommandLineOptions.Clo.XmlSink != null)
            {
                string errMsg = CommandLineOptions.Clo.XmlSink.Open();
                if (errMsg != null)
                {
                    ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: " + errMsg);
                    return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                }
            }
            if (CommandLineOptions.Clo.ShowEnv == CommandLineOptions.ShowEnvironment.Always)
            {
                Console.WriteLine("---Command arguments");
                foreach (string arg in args)
                {
                    Contract.Assert(arg != null);
                    Console.WriteLine(arg);
                }
                Console.WriteLine("--------------------");
            }

            ISet <String> filesSeen = new HashSet <string>();

            foreach (string file in CommandLineOptions.Clo.Files)
            {
                Contract.Assert(file != null);
                string extension = Path.GetExtension(file);
                if (extension != null)
                {
                    extension = extension.ToLower();
                }

                bool isDafnyFile = false;
                try {
                    var df = new DafnyFile(file);
                    if (!filesSeen.Add(df.CanonicalPath))
                    {
                        continue; // silently ignore duplicate
                    }
                    dafnyFiles.Add(df);
                    isDafnyFile = true;
                } catch (IllegalDafnyFile) {
                    // Fall through and try to handle the file as an "other file"
                }

                if (DafnyOptions.O.CompileTarget == DafnyOptions.CompilationTarget.Csharp)
                {
                    if (extension == ".cs" || extension == ".dll")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        if (extension == "" && file.Length > 0 && (file[0] == '/' || file[0] == '-'))
                        {
                            ExecutionEngine.printer.ErrorWriteLine(Console.Out,
                                                                   "*** Error: Command-line argument '{0}' is neither a recognized option nor a filename with a supported extension (.dfy, .cs, .dll).",
                                                                   file);
                        }
                        else
                        {
                            ExecutionEngine.printer.ErrorWriteLine(Console.Out,
                                                                   "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or C# files (.cs) or managed DLLS (.dll)",
                                                                   file,
                                                                   extension == null ? "" : extension);
                        }
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
                else if (DafnyOptions.O.CompileTarget == DafnyOptions.CompilationTarget.JavaScript)
                {
                    if (extension == ".js")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or JavaScript files (.js)", file,
                                                               extension == null ? "" : extension);
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
                else if (DafnyOptions.O.CompileTarget == DafnyOptions.CompilationTarget.Java)
                {
                    if (extension == ".java")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or Java files (.java)", file,
                                                               extension == null ? "" : extension);
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
                else if (DafnyOptions.O.CompileTarget == DafnyOptions.CompilationTarget.Cpp)
                {
                    if (extension == ".h")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or C headers (.h)", file,
                                                               extension == null ? "" : extension);
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
                else if (DafnyOptions.O.CompileTarget == DafnyOptions.CompilationTarget.Php)
                {
                    if (extension == ".php")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or PHP files (.php)", file,
                                                               extension == null ? "" : extension);
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
                else
                {
                    if (extension == ".go")
                    {
                        otherFiles.Add(file);
                    }
                    else if (!isDafnyFile)
                    {
                        ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: '{0}': Filename extension '{1}' is not supported. Input files must be Dafny programs (.dfy) or Go files (.go)", file,
                                                               extension == null ? "" : extension);
                        return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
                    }
                }
            }
            if (dafnyFiles.Count == 0)
            {
                ExecutionEngine.printer.ErrorWriteLine(Console.Out, "*** Error: The command-line contains no .dfy files");
                return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
            }

            if (dafnyFiles.Count > 1 &&
                DafnyOptions.O.TestGenOptions.Mode != TestGenerationOptions.Modes.None)
            {
                ExecutionEngine.printer.ErrorWriteLine(Console.Out,
                                                       "*** Error: Only one .dfy file can be specified for testing");
                return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
            }

            if (DafnyOptions.O.ExtractCounterexample && DafnyOptions.O.ModelViewFile == null)
            {
                ExecutionEngine.printer.ErrorWriteLine(Console.Out,
                                                       "*** Error: ModelView file must be specified when attempting counterexample extraction");
                return(CommandLineArgumentsResult.PREPROCESSING_ERROR);
            }
            return(CommandLineArgumentsResult.OK);
        }