[vala/staging: 5/5] vala: Add CodeContext.get_source_file() and perform some sanity checks



commit 67a8ac1983aca5217b5d1c733594a89c86b662e6
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Mar 19 13:42:13 2019 +0100

    vala: Add CodeContext.get_source_file() and perform some sanity checks

 vala/valacodecontext.vala | 23 +++++++++++++++++++++++
 vala/valasourcefile.vala  |  2 +-
 vapigen/valavapigen.vala  |  7 +++----
 3 files changed, 27 insertions(+), 5 deletions(-)
---
diff --git a/vala/valacodecontext.vala b/vala/valacodecontext.vala
index 8b1a78be4..42e6dabb2 100644
--- a/vala/valacodecontext.vala
+++ b/vala/valacodecontext.vala
@@ -203,6 +203,7 @@ public class Vala.CodeContext {
        public string[] gresources_directories { get; set; default = {}; }
 
        private List<SourceFile> source_files = new ArrayList<SourceFile> ();
+       private Map<string,unowned SourceFile> source_files_map = new HashMap<string,unowned SourceFile> 
(str_hash, str_equal);
        private List<string> c_source_files = new ArrayList<string> ();
        private Namespace _root = new Namespace (null);
 
@@ -302,7 +303,23 @@ public class Vala.CodeContext {
         * @param file a source file
         */
        public void add_source_file (SourceFile file) {
+               if (source_files_map.contains (file.filename)) {
+                       Report.warning (null, "Ignoring source file `%s', which was already added to this 
context".printf (file.filename));
+                       return;
+               }
+
                source_files.add (file);
+               source_files_map.set (file.filename, file);
+       }
+
+       /**
+        * Returns the source file for a given path.
+        *
+        * @param filename a path to a source file
+        * @return the source file if found
+        */
+       public unowned Vala.SourceFile? get_source_file (string filename) {
+               return source_files_map.get (filename);
        }
 
        /**
@@ -445,11 +462,17 @@ public class Vala.CodeContext {
                        }
 
                        add_source_file (source_file);
+                       if (rpath != filename) {
+                               source_files_map.set (filename, source_file);
+                       }
                } else if (filename.has_suffix (".vapi") || filename.has_suffix (".gir")) {
                        var source_file = new SourceFile (this, SourceFileType.PACKAGE, rpath, null, cmdline);
                        source_file.relative_filename = filename;
 
                        add_source_file (source_file);
+                       if (rpath != filename) {
+                               source_files_map.set (filename, source_file);
+                       }
                } else if (filename.has_suffix (".c")) {
                        add_c_source_file (rpath);
                } else if (filename.has_suffix (".h")) {
diff --git a/vala/valasourcefile.vala b/vala/valasourcefile.vala
index 35eaf5f26..4dca01224 100644
--- a/vala/valasourcefile.vala
+++ b/vala/valasourcefile.vala
@@ -29,7 +29,7 @@ public class Vala.SourceFile {
        /**
         * The name of this source file.
         */
-       public string filename { get; set; }
+       public string filename { get; private set; }
 
        public string? relative_filename {
                set {
diff --git a/vapigen/valavapigen.vala b/vapigen/valavapigen.vala
index 63205499d..ef0b37200 100644
--- a/vapigen/valavapigen.vala
+++ b/vapigen/valavapigen.vala
@@ -184,10 +184,9 @@ class Vala.VAPIGen {
                                        // mark relative metadata as source
                                        string? metadata_filename = context.get_metadata_path (file.filename);
                                        if (metadata_filename != null) {
-                                               foreach (SourceFile metadata_file in context.get_source_files 
()) {
-                                                       if (metadata_file.filename == metadata_filename) {
-                                                               metadata_file.file_type = 
SourceFileType.SOURCE;
-                                                       }
+                                               unowned SourceFile? metadata_file = context.get_source_file 
(metadata_filename);
+                                               if (metadata_file != null) {
+                                                       metadata_file.file_type = SourceFileType.SOURCE;
                                                }
                                        }
                                        if (file.from_commandline && file.package_name != null) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]